import os
from IPython.display import HTML, display
Module 1¶
#print(ModuleFolder)
#lnk="/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork_labs_Cheat_Sheet_Week-1.md.html"
ModuleFolder='C:/Users/Gamaliel/Documents/G/ADD/IBM_DS/Python4DS/IBMs-NBs/Mod1/'
os.chdir(ModuleFolder)
for file in os.listdir(ModuleFolder):
if file.lower().endswith('HTMLs.html'):
continue
elif file.lower().endswith('.html'):
link=file
#print(link)
display(HTML(ModuleFolder+link))
elif file.lower().endswith('.htm'):
link=file
#print(link)
display(HTML(ModuleFolder+link))
else:
continue
#for file in os.listdir(ModuleFolder):
# if not file.lower().endswith('.htm'):
# continue
# link=file
#print(link)
#display(HTML(ModuleFolder+link))
Module 1 Cheat Sheet: Python Basics
| Package/Method | Description | Code Example |
|---|---|---|
| Comments | Comments are lines of text that are ignored by the Python interpreter when executing the code<./td> |
|
| Concatenation | Combines (concatenates) strings. | Syntax:
Example:
|
| Data Types | - Integer - Float - Boolean - String | Example:
|
| Indexing | Accesses character at a specific index. | Example:
|
| len() | Returns the length of a string. | Syntax:
Example:
|
| lower() | Converts string to lowercase. | Example:
|
| print() | Prints the message or variable inside `()`. | Example:
|
| Python Operators |
- Addition (+): Adds two values together. - Subtraction (-): Subtracts one value from another. - Multiplication (*): Multiplies two values. - Division (/): Divides one value by another, returns a float. - Floor Division (//): Divides one value by another, returns the quotient as an integer. - Modulo (%): Returns the remainder after division. |
Example:
|
| replace() | Replaces substrings. | Example:
|
| Slicing | Extracts a portion of the string. | Syntax:
Example:
|
| split() | Splits string into a list based on a delimiter. | Example:
|
| strip() | Removes leading/trailing whitespace. | Example:
|
| upper() | Converts string to uppercase. | Example:
|
| Variable Assignment | Assigns a value to a variable. | Syntax:
Example:
|
© IBM Corporation. All rights reserved.
Module 2¶
#print(ModuleFolder)
#lnk="/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork_labs_Cheat_Sheet_Week-1.md.html"
ModuleFolder='C:/Users/Gamaliel/Documents/G/ADD/IBM_DS/Python4DS/IBMs-NBs/Mod2/'
for file in os.listdir(ModuleFolder):
if file.lower().endswith('HTMLs.html'):
continue
elif file.lower().endswith('.html'):
link=file
#print(link)
display(HTML(ModuleFolder+link))
elif file.lower().endswith('.htm'):
link=file
#print(link)
display(HTML(ModuleFolder+link))
else:
continue
#for file in os.listdir(ModuleFolder):
# if not file.lower().endswith('.htm'):
# continue
# link=file
#print(link)
#display(HTML(ModuleFolder+link))
Glossary: Python Basics
Welcome! This alphabetized glossary contains many of the terms you'll find within this course. This comprehensive glossary also includes additional industry-recognized terms not used in course videos. These terms are important for you to recognize when working in the industry, participating in user groups, and participating in other certificate programs.
| Term | Definition |
|---|---|
| AI | AI (artificial intelligence) is the ability of a digital computer or computer-controlled robot to perform tasks commonly associated with intelligent beings. |
| Application development | Application development, or app development, is the process of planning, designing, creating, testing, and deploying a software application to perform various business operations. |
| Arithmetic Operations | Arithmetic operations are the basic calculations we make in everyday life like addition, subtraction, multiplication and division. It is also called as algebraic operations or mathematical operations. |
| Array of numbers | Set of numbers or objects that follow a pattern presented as an arrangement of rows and columns to explain multiplication. |
| Assignment operator in Python | Assignment operator is a type of Binary operator that helps in modifying the variable to its left with the use of its value to the right. The symbol used for assignment operator is "=". |
| Asterisk | Symbol "* " used to perform various operations in Python. |
| Backslash | A backslash is an escape character used in Python strings to indicate that the character immediately following it should be treated in a special way, such as being treated as escaped character or raw string. |
| Boolean | Denoting a system of algebraic notation used to represent logical propositions by means of the binary digits 0 (false) and 1 (true). |
| Colon | A colon is used to represent an indented block. It is also used to fetch data and index ranges or arrays. |
| Concatenate | Link (things) together in a chain or series. |
| Data engineering | Data engineers are responsible for turning raw data into information that an organization can understand and use. Their work involves blending, testing, and optimizing data from numerous sources. |
| Data science | Data Science is an interdisciplinary field that focuses on extracting knowledge from data sets which are typically huge in amount. The field encompasses analysis, preparing data for analysis, and presenting findings to inform high-level decisions in an organization. |
| Data type | Data type refers to the type of value a variable has and what type of mathematical, relational or logical operations can be applied without causing an error. |
| Double quote | Symbol “ “ used to represent strings in Python. |
| Escape sequence | An escape sequence is two or more characters that often begin with an escape character that tell the computer to perform a function or command. |
| Expression | An expression is a combination of operators and operands that is interpreted to produce some other value. |
| Float | Python float () function is used to return a floating-point number from a number or a string representation of a numeric value. |
| Forward slash | Symbol “/“ used to perform various operation sin Python |
| Foundational | Denoting an underlying basis or principle; fundamental. |
| Immutable | Immutable Objects are of in-built datatypes like int, float, bool, string, Unicode, and tuple. In simple words, an immutable object can’t be changed after it is created. |
| Integer | An integer is the number zero (0), a positive natural number (1, 2, 3, and so on) or a negative integer with a minus sign (−1, −2, −3, and so on.) |
| Manipulate | Is the process of modifying a string or creating a new string by making changes to existing strings. |
| Mathematical conventions | A mathematical convention is a fact, name, notation, or usage which is generally agreed upon by mathematicians. |
| Mathematical expressions | Expressions in math are mathematical statements that have a minimum of two terms containing numbers or variables, or both, connected by an operator in between. |
| Mathematical operations | The mathematical “operation” refers to calculating a value using operands and a math operator. |
| Negative indexing | Allows you to access elements of a sequence (such as a list, a string, or a tuple) from the end, using negative numbers as indexes. |
| Operands | The quantity on which an operation is to be done. |
| Operators in Python | Operators are used to perform operations on variables and values. |
| Parentheses | Parentheses is used to call an object. |
| Replicate | To make an exact copy of. |
| Sequence | A sequence is formally defined as a function whose domain is an interval of integers. |
| Single quote | Symbol ‘ ‘ used to represent strings in python. |
| Slicing in Python | Slicing is used to return a portion from defined list. |
| Special characters | A special character is one that is not considered a number or letter. Symbols, accent marks, and punctuation marks are considered special characters. |
| Stride value | Stride is the number of bytes from one row of pixels in memory to the next row of pixels in memory. |
| Strings | In Python, Strings are arrays of bytes representing Unicode characters. |
| Substring | A substring is a sequence of characters that are part of an original string. |
| Type casting | The process of converting one data type to another data type is called Typecasting or Type Coercion or Type Conversion. |
| Types in Python | Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. |
| Variables | Variables are containers for storing data values. |

Python Data Structures Cheat Sheet
List
| Package/Method | Description | Code Example |
|---|---|---|
| append() | The `append()` method is used to add an element to the end of a list. | Syntax:
Example:
|
| copy() | The `copy()` method is used to create a shallow copy of a list. | Example 1:
|
| count() | The `count()` method is used to count the number of occurrences of a specific element in a list in Python. | Example:
|
| Creating a list | A list is a built-in data type that represents an ordered and mutable collection of elements. Lists are enclosed in square brackets [] and elements are separated by commas. | Example:
|
| del | The `del` statement is used to remove an element from list. `del` statement removes the element at the specified index. | Example:
|
| extend() | The `extend()` method is used to add multiple elements to a list. It takes an iterable (such as another list, tuple, or string) and appends each element of the iterable to the original list. | Syntax:
Example:
|
| Indexing | Indexing in a list allows you to access individual elements by their position. In Python, indexing starts from 0 for the first element and goes up to `length_of_list - 1`. | Example:
|
| insert() | The `insert()` method is used to insert an element. | Syntax:
Example:
|
| Modifying a list | You can use indexing to modify or assign new values to specific elements in the list. | Example:
|
| pop() | `pop()` method is another way to remove an element from a list in Python. It removes and returns the element at the specified index. If you don't provide an index to the `pop()` method, it will remove and return the last element of the list by default | Example 1:
Example 2:
|
| remove() | To remove an element from a list. The `remove()` method removes the first occurrence of the specified value. | Example:
|
| reverse() | The `reverse()` method is used to reverse the order of elements in a list | Example 1:
|
| Slicing | You can use slicing to access a range of elements from a list. | Syntax:
Example:
|
| sort() | The `sort()` method is used to sort the elements of a list in ascending order. If you want to sort the list in descending order, you can pass the `reverse=True` argument to the `sort()` method. | Example 1:
Example 2:
|
| Package/Method | Description | Code Example |
|---|---|---|
| count() | The count() method for a tuple is used to count how many times a specified element appears in the tuple. | Syntax:
Example:
|
| index() | The index() method in a tuple is used to find the first occurrence of a specified value and returns its position (index). If the value is not found, it raises a ValueError. | Syntax:
Example:
|
| sum() | The sum() function in Python can be used to calculate the sum of all elements in a tuple, provided that the elements are numeric (integers or floats). | Syntax:
Example:
|
| min() and max() | Find the smallest (min()) or largest (max()) element in a tuple. | Example:
|
| len() | Get the number of elements in the tuple using len(). | Syntax:
Example:
|
© IBM Corporation. All rights reserved.
Cheat Sheet: Python Data Structures Part-2
Dictionaries
| Package/Method | Description | Code Example |
|---|---|---|
| Creating a Dictionary |
A dictionary is a built-in data type that represents a collection of key-value pairs. Dictionaries are enclosed in curly braces |
Example:
|
| Accessing Values |
You can access the values in a dictionary using their corresponding |
Syntax:
Example:
|
|
Add or modify |
Inserts a new key-value pair into the dictionary. If the key already exists, the value will be updated; otherwise, a new entry is created. |
Syntax:
Example:
|
| del |
Removes the specified key-value pair from the dictionary. Raises a |
Syntax:
Example:
|
| update() |
The |
Syntax:
Example:
|
| clear() |
The |
Syntax:
Example:
|
| key existence |
You can check for the existence of a key in a dictionary using the |
Example:
|
| copy() |
Creates a shallow copy of the dictionary. The new dictionary contains the same key-value pairs as the original, but they remain distinct objects in memory. |
Syntax:
Example:
|
| keys() |
Retrieves all keys from the dictionary and converts them into a list. Useful for iterating or processing keys using list methods. |
Syntax:
Example:
|
| values() |
Extracts all values from the dictionary and converts them into a list. This list can be used for further processing or analysis. |
Syntax:
Example:
|
| items() |
Retrieves all key-value pairs as tuples and converts them into a list of tuples. Each tuple consists of a key and its corresponding value. |
Syntax:
Example:
|
Sets
| Package/Method | Description | Code Example |
|---|---|---|
| add() | Elements can be added to a set using the `add()` method. Duplicates are automatically removed, as sets only store unique values. | Syntax:
Example:
|
| clear() | The `clear()` method removes all elements from the set, resulting in an empty set. It updates the set in-place. | Syntax:
Example:
|
| copy() | The `copy()` method creates a shallow copy of the set. Any modifications to the copy won't affect the original set. | Syntax:
Example:
|
| Defining Sets | A set is an unordered collection of unique elements. Sets are enclosed in curly braces `{}`. They are useful for storing distinct values and performing set operations. | Example:
|
| discard() | Use the `discard()` method to remove a specific element from the set. Ignores if the element is not found. | Syntax:
Example:
|
| issubset() | The `issubset()` method checks if the current set is a subset of another set. It returns True if all elements of the current set are present in the other set, otherwise False. | Syntax:
Example:
|
| issuperset() | The `issuperset()` method checks if the current set is a superset of another set. It returns True if all elements of the other set are present in the current set, otherwise False. |
Syntax:
Example:
|
| pop() | The `pop()` method removes and returns an arbitrary element from the set. It raises a `KeyError` if the set is empty. Use this method to remove elements when the order doesn't matter. | Syntax:
Example:
|
| remove() | Use the `remove()` method to remove a specific element from the set. Raises a `KeyError` if the element is not found. | Syntax:
Example:
|
| Set Operations | Perform various operations on sets: `union`, `intersection`, `difference`, `symmetric difference`. | Syntax:
Example:
|
| update() | The `update()` method adds elements from another iterable into the set. It maintains the uniqueness of elements. | Syntax:
Example:
|
© IBM Corporation. All rights reserved.
Glossary: Python Data Structures
Welcome! This alphabetized glossary contains many of the terms in this course. This comprehensive glossary also includes additional industry-recognized terms not used in course videos. These terms are important for you to recognize when working in the industry, participating in user groups, and participating in other certificate programs.
| Term | Definition |
|---|---|
| Aliasing | Aliasing refers to giving another name to a function or a variable. |
| Ampersand | A character typically "&" standing for the word "and." |
| Compound elements | Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. |
| Delimiter | A delimiter in Python is a character or sequence of characters used to separate or mark the boundaries between elements or fields within a larger data structure, such as a string or a file. |
| Dictionaries | A dictionary in Python is a data structure that stores a collection of key-value pairs, where each key is unique and associated with a specific value. |
| Function | A function is a block of code, defining a set procedure, which is executed only when it is called. |
| Immutable | Immutable Objects are of in-built datatypes like int, float, bool, string, Unicode, and tuple. In simple words, an immutable object can't be changed after it is created. |
| Intersection | The intersection of two sets is a new set containing only the elements that are present in both sets. |
| Keys | The keys () method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion using Python. |
| Lists | A list is any list of data items, separated by commas, inside square brackets. |
| Logic operations | In Python, logic operations refer to the use of logical operators such as "and," "or," and "not" to perform logical operations on Boolean values (True or False). |
| Mutable | Mutable objects in Python are objects whose values can be changed after they are created. These objects allow modifications such as adding, removing, or altering elements without creating a new object. |
| Nesting | A nested function is simply a function within another function and is sometimes called an "inner function". |
| Ratings in python | Ratings in Python typically refer to a numerical or qualitative measure assigned to something to indicate its quality, performance, or value. |
| Set operations | Set operations in Python refer to mathematical operations performed on sets, which are unordered collections of unique elements. |
| Sets in python | A set is an unordered collection of unique elements. |
| Syntax | The rules that define the structure of the language for python is called its syntax. |
| Tuples | These are used store multiple items in a single variable. |
| Type casting | In python, this is converting one data type to another. |
| Variables | In python, a variable is a symbolic name or identifier used to store and manipulate data. Variables serve as containers for values, and these values can be of various data types, including numbers, strings, lists, and more. |
| Venn diagram | A Venn diagram is a graphical representation that uses overlapping circles to illustrate the relationships and commonalities between sets or groups of items. |
| Versatile data | Versatile data, in a general context, refers to data that can be used in multiple ways, is adaptable to different applications or purposes, and is not restricted to a specific use case. |

Conditions and Branching
Estimated time needed: 10 minutes
Objective:
In this reading, you'll learn about:
- Comparison operators
- Branching
- Logical operators
1. Comparison operations
Comparison operations are essential in programming. They help compare values and make decisions based on the results.
Equality operator
The equality operator == checks if two values are equal. For example, in Python:
- 1
- 2
- 3
age = 25if age == 25:print("You are 25 years old.")
Here, the code checks if the variable age is equal to 25 and prints a message accordingly.
Inequality operator
The inequality operator != checks if two values are not equal:
- 1
- 2
if age != 30:print("You are not 30 years old.")
Here, the code checks if the variable age is not equal to 30 and prints a message accordingly.
Greater than and less than
You can also compare if one value is greater than another.
- 1
- 2
if age>= 20:Print("Yes, the Age is greater than 20")
Here, the code checks if the variable age is greater than or equal to 20 and prints a message accordingly.
2. Branching
Branching is like making decisions in your program based on conditions. Think of it as real-life choices.
The IF statement
Consider a real-life scenario of entering a bar. If you're above a certain age, you can enter; otherwise, you cannot.
- 1
- 2
- 3
- 4
- 5
age = 20if age >= 21:print("You can enter the bar.")else:print("Sorry, you cannot enter.")
Here, you are using the if statement to make a decision based on the age variable.
The ELIF Statement
Sometimes, there are multiple conditions to check. For example, if you're not old enough for the bar, you can go to a movie instead.
- 1
- 2
- 3
- 4
- 5
- 6
if age >= 21:print("You can enter the bar.")elif age >= 18:print("You can watch a movie.")else:print("Sorry, you cannot do either.")
Real-life example: Automated Teller Machine (ATM)
When a user interacts with an ATM, the software in the ATM can use branching to make decisions based on the user's input. For example, if the user selects "Withdraw Cash" the ATM can branch into different denominations of bills to dispense based on the amount requested.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
user_choice = "Withdraw Cash"if user_choice == "Withdraw Cash":amount = input("Enter the amount to withdraw: ")if amount % 10 == 0:dispense_cash(amount)else:print("Please enter a multiple of 10.")else:print("Thank you for using the ATM.")
3. Logical operators
Logical operators help combine and manipulate conditions.
The NOT operator
Real-life example: Notification settings
In a smartphone's notification settings, you can use the NOT operator to control when to send notifications. For example, you might only want to receive notifications when your phone is not in "Do Not Disturb" mode.
The not operator negates a condition.
- 1
- 2
- 3
is_do_not_disturb = Trueif not is_do_not_disturb:send_notification("New message received")
The AND operator
Real-life example: Access control
In a secure facility, you can use the AND operator to check multiple conditions for access. To open a high-security door, a person might need both a valid ID card and a matching fingerprint.
The AND operator checks if all required conditions are true, like needing both keys to open a safe.
- 1
- 2
- 3
- 4
has_valid_id_card = Truehas_matching_fingerprint = Trueif has_valid_id_card and has_matching_fingerprint:open_high_security_door()
The OR operator
Real-life example: Movie night decision
When planning a movie night with friends, you can use the OR operator to decide on a movie genre. You'll choose a movie if at least one person is interested.
The OR operator checks if at least one condition is true. It's like choosing between different movies to watch.
- 1
- 2
- 3
- 4
- 5
friend1_likes_comedy = Truefriend2_likes_action = Falsefriend3_likes_drama = Falseif friend1_likes_comedy or friend2_likes_action or friend3_likes_drama:choose a movie()
Summary
In
this reading, you delved into the most frequently used operator and the
concept of conditional branching, which encompasses the utilization of if statements and if-else statements.
Author

Module 3¶
#print(ModuleFolder)
#lnk="/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork_labs_Cheat_Sheet_Week-1.md.html"
ModuleFolder='C:/Users/Gamaliel/Documents/G/ADD/IBM_DS/Python4DS/IBMs-NBs/Mod3/'
for file in os.listdir(ModuleFolder):
if file.lower().endswith('HTMLs.html'):
continue
elif file.lower().endswith('.html'):
link=file
#print(link)
display(HTML(ModuleFolder+link))
elif file.lower().endswith('.htm'):
link=file
#print(link)
display(HTML(ModuleFolder+link))
else:
continue
#for file in os.listdir(ModuleFolder):
# if not file.lower().endswith('.htm'):
# continue
# link=file
#print(link)
#display(HTML(ModuleFolder+link))
Introduction to Loops in Python
Estimated time needed: 10 minutes
Objectives
- Understand Python loops.
- How the loop Works
- Learn about the needs for loop
- Utilize Python's Range function.
- Familiarize with Python's enumerate function.
- Apply while loops for conditional tasks.
- Distinguish appropriate loop selection.
What is a Loop?
In programming, a loop is like a magic trick that allows a computer to do something over and over again. Imagine you are a magician's assistant, and your magician friend asks you to pull a rabbit out of a hat, but not just once - they want you to keep doing it until they tell you to stop. That is what loops do for computers - they repeat a set of instructions as many times as needed.
How Loop works?
Here's how it works in Python:

Start: The for loop begins with the keyword for, followed by a variable that will take on each value in a sequence.
Condition: After the variable, you specify the keyword in and a sequence, such as a list or a range, that the loop will iterate through.
If Condition True:
- The loop takes the first value from the sequence and assigns it to the variable.
- The indented block of code following the loop header is executed using this value.
- The loop then moves to the next value in the sequence and repeats the process until all values have been used.
Statement: Inside the indented block of the loop, you write the statements that you want to repeat for each value in the sequence.
Repeat: The loop continues to repeat the block of code for each value in the sequence until there are no more values left.
If Condition False:
- Once all values in the sequence have been processed, the loop terminates automatically.
- The loop completes its execution, and the program continues to the next statement after the loop.
The Need for Loops
Think about when you need to count from 1 to 10. Doing it manually is easy, but what if you had to count to a million? Typing all those numbers one by one would be a nightmare! This is where loops come in handy. They help computers repeat tasks quickly and accurately without getting tired.
Main Types of Loops
For Loops
For loops are like a superhero's checklist. A for loop in programming is a control structure that allows the repeated execution of a set of statements for each item in a sequence, such as elements in a list or numbers in a range, enabling efficient iteration and automation of tasks
Syntax of for loop
- 1
- 2
for val in sequence:# statement(s) to be executed in sequence as a part of the loop.
Here is an example of For loop.
Imagine you're a painter, and you want to paint a beautiful rainbow with seven colors. Instead of picking up each color one by one and painting the rainbow, you could tell a magical painter's assistant to do it for you. This is what a basic for loop does in programming.
We have a list of colours.
- 1
colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
Let's print the colour name in the new line using for loop.
- 1
- 2
for color in colors:print(color)
In this example, the for loop picks each color from the colors list and prints it on the screen. You don't have to write the same code for each color - the loop does it automatically!
Sometimes you do not want to paint a rainbow, but you want to count the number of steps to reach your goal. A range-based for loop is like having a friendly step counter that helps you reach your target.
Here is how you might use a for loop to count from 1 to 10:
- 1
- 2
for number in range(1, 11):print(number)
Here, the range(1, 11) generates a sequence from 1 to 10, and the for loop goes through each number in that sequence, printing it out. It's like taking 10 steps, and you're guided by the loop!
Range Function
The range function in Python generates an ordered sequence that can be used in loops. It takes one or two arguments:
- If given one argument (e.g., range(11)), it generates a sequence starting from 0 up to (but not including) the given number.
- 1
- 2
for number in range(11):print(number)
- If given two arguments (e.g., range(1, 11)), it generates a sequence starting from the first argument up to (but not including) the second argument.
- 1
- 2
for number in range(1, 11):print(number)
The Enumerated For Loop
Have you ever needed to keep track of both the item and its position in a list? An enumerated for loop comes to your rescue. It's like having a personal assistant who not only hands you the item but also tells you where to find it.
Consider this example:
- 1
- 2
- 3
fruits = ["apple", "banana", "orange"]for index, fruit in enumerate(fruits):print(f"At position {index}, I found a {fruit}")
With this loop, you not only get the fruit but also its position in the list. It's as if you have a magical guide pointing out each fruit's location!
While Loops
While
loops are like a sleepless night at a friend's sleepover. Imagine you
and your friends keep telling ghost stories until someone decides it's
time to sleep. As long as no one says, "Let's sleep" you keep telling
stories.
A while loop works similarly - it repeats a task as long as a
certain condition is true. It's like saying, "Hey computer, keep doing
this until I say stop!"
Basic syntax of While Loop.
- 1
- 2
- 3
while condition:# Code to be executed while the condition is true# Indentation is crucial to indicate the scope of the loop
For example, here's how you might use a while loop to count from 1 to 10:
- 1
- 2
- 3
- 4
count = 1while count <= 10:print(count)count += 1
here's a breakdown of the above code.
There is a variable named count initialized with the value 1.
The while loop is used to repeatedly execute a block of code as long as a given condition is True. In this case, the condition is count <= 10, meaning the loop will continue as long as count is less than or equal to 10.
Inside the loop:
The print(count) statement outputs the current value of the count variable.
The count += 1 statement increments the value of count by 1. This step ensures that the loop will eventually terminate when count becomes greater than 10.
The loop will continue executing as long as the condition count <= 10 is satisfied.
The loop will print the numbers 1 to 10 in consecutive order since the print statement is inside the loop block and executed during each iteration.
Once count reaches 11, the condition count <= 10 will evaluate to False, and the loop will terminate.
The output of the code will be the numbers 1 to 10, each printed on a separate line.
The Loop Flow
Both for and while loops have their special moves, but they follow a pattern:
Initialization: You set up things like a starting point or conditions.
Condition: You decide when the loop should keep going and when it should stop.
Execution: You do the task inside the loop.
Update: You make changes to your starting point or conditions to move forward.
Repeat: The loop goes back to step 2 until the condition is no longer true.
When to Use Each
For Loops: Use for loops when you know the number of iterations in advance and want to process each element in a sequence. They are best suited for iterating over collections and sequences where the length is known.
While Loops: Use while loops when you need to perform a task repeatedly as long as a certain condition holds true. While loops are particularly useful for situations where the number of iterations is uncertain or where you're waiting for a specific condition to be met.
Summary
In this adventure into coding, we explored loops in Python - special tools that help us do things over and over again without getting tired. We met two types of loops: "for loops" and "while loops."
For Loops were like helpers that made us repeat tasks in order. We painted colors, counted numbers, and even got a helper to tell us where things were in a list. For loops made our job easier and made our code look cleaner.
While Loops were like detectives that kept doing something as long as a rule was true. They helped us take steps, guess numbers, and work until we were tired. While loops were like smart assistants that didn't stop until we said so.
Author(s)
Changelog
| Date | Version | Changed by | Change Description |
|---|---|---|---|
| 2023-21-08 | 1.0 | Akansha Yadav | Created a reading file |
Exploring Python Functions
Estimated time needed: 15 minutes
Objectives:
By the end of this reading, you should be able to:
- Describe the function concept and the importance of functions in programming
- Write a function that takes inputs and performs tasks
- Use built-in functions like len(), sum(), and others effectively
- Define and use your functions in Python
- Differentiate between global and local variable scopes
- Use loops within the function
- Modify data structures using functions
Introduction to functions
A function is a fundamental building block that encapsulates specific actions or computations. As in mathematics, where functions take inputs and produce outputs, programming functions perform similarly. They take inputs, execute predefined actions or calculations, and then return an output.
Purpose of functions
Functions promote code modularity and reusability. Imagine you have a task that needs to be performed multiple times within a program. Instead of duplicating the same code at various places, you can define a function once and call it whenever you need that task. This reduces redundancy and makes the code easier to manage and maintain.
Benefits of using functions
Modularity: Functions break down complex tasks into manageable components
Reusability: Functions can be used multiple times without rewriting code
Readability: Functions with meaningful names enhance code understanding
Debugging: Isolating functions eases troubleshooting and issue fixing
Abstraction: Functions simplify complex processes behind a user-friendly interface
Collaboration: Team members can work on different functions concurrently
Maintenance: Changes made in a function automatically apply wherever it's used
How functions take inputs, perform tasks, and produce outputs
Inputs (Parameters)
Functions operate on data, and they can receive data as input. These inputs are known as parameters or arguments. Parameters provide functions with the necessary information they need to perform their tasks. Consider parameters as values you pass to a function, allowing it to work with specific data.
Performing tasks
Once a function receives its input (parameters), it executes predefined actions or computations. These actions can include calculations, operations on data, or even more complex tasks. The purpose of a function determines the tasks it performs. For instance, a function could calculate the sum of numbers, sort a list, format text, or fetch data from a database.
Producing outputs
After performing its tasks, a function can produce an output. This output is the result of the operations carried out within the function. It's the value that the function “returns” to the code that called it. Think of the output as the end product of the function's work. You can use this output in your code, assign it to variables, pass it to other functions, or even print it out for display.
Example:
Consider a function named calculate_total that takes two numbers as input (parameters), adds them together, and then produces the sum as the output. Here's how it works:
- 1
- 2
- 3
- 4
- 5
- 6
def calculate_total(a, b): # Parameters: a and btotal = a + b # Task: Additionreturn total # Output: Sum of a and bresult = calculate_total(5, 7) # Calling the function with inputs 5 and 7print(result) # Output: 12
Python's built-in functions
Python has a rich set of built-in functions that provide a wide range of functionalities. These functions are readily available for you to use, and you don't need to be concerned about how they are implemented internally. Instead, you can focus on understanding what each function does and how to use it effectively.
Using built-in functions or Pre-defined functions
To use a built-in function, you simply call the function's name followed by parentheses. Any required arguments or parameters are passed into the function within these parentheses. The function then performs its predefined task and may return an output you can use in your code.
Here are a few examples of commonly used built-in functions:
len(): Calculates the length of a sequence or collection
- 1
- 2
string_length = len("Hello, World!") # Output: 13list_length = len([1, 2, 3, 4, 5]) # Output: 5
sum(): Adds up the elements in an iterable (list, tuple, and so on)
- 1
total = sum([10, 20, 30, 40, 50]) # Output: 150
max(): Returns the maximum value in an iterable
- 1
highest = max([5, 12, 8, 23, 16]) # Output: 23
min(): Returns the minimum value in an iterable
- 1
lowest = min([5, 12, 8, 23, 16]) # Output: 5
Python's built-in functions offer a wide array of functionalities, from basic operations like len() and sum() to more specialized tasks.
Defining your functions
Defining a function is like creating your mini-program:
- Use
deffollowed by the function name and parentheses
Here is the syntax to define a function:
- 1
- 2
def function_name():pass
A "pass" statement in a programming
function is a placeholder or a no-op (no operation) statement. Use it
when you want to define a function or a code block syntactically but do
not want to specify any functionality or implementation at that moment.
Placeholder:
"pass"acts as a temporary placeholder for future code that you intend to write within a function or a code block.Syntax Requirement: In many programming languages like Python, using
"pass"is necessary when you define a function or a conditional block. It ensures that the code remains syntactically correct, even if it doesn't do anything yet.No Operation:
"pass"itself doesn't perform any meaningful action. When the interpreter encounters “pass”, it simply moves on to the next statement without executing any code.
Function Parameters:
- Parameters are like inputs for functions
- They go inside parentheses when defining the function
- Functions can have multiple parameters
Example:
- 1
- 2
- 3
- 4
- 5
def greet(name):return "Hello, " + nameresult = greet("Alice")print(result) # Output: Hello, Alice
Docstrings (Documentation Strings)
- Docstrings explain what a function does
- Placed inside triple quotes under the function definition
- Helps other developers understand your function
Example:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
def multiply(a, b):"""This function multiplies two numbers.Input: a (number), b (number)Output: Product of a and b"""print(a * b)multiply(2,6)
Return statement
- Return gives back a value from a function
- Ends the function's execution and sends the result
- A function can return various types of data
Example:
- 1
- 2
- 3
- 4
def add(a, b):return a + bsum_result = add(3, 5) # sum_result gets the value 8
Understanding scopes and variables
Scope is where a variable can be seen and used:
- Global Scope: Variables defined outside functions; accessible everywhere
- Local Scope: Variables inside functions; only usable within that function
Example:
Part 1: Global variable declaration
- 1
global_variable = "I'm global"
This line initializes a global variable called global_variable and assigns it the value "I'm global".
Global variables are accessible throughout the entire program, both inside and outside functions.
Part 2: Function definition
- 1
- 2
- 3
- 4
def example_function():local_variable = "I'm local"print(global_variable) # Accessing global variableprint(local_variable) # Accessing local variable
Here, you define a function called example_function().
Within this function:
A local variable named local_variable is declared and initialized with the string value "I'm local." This variable is local to the function and can only be accessed within the function's scope.
The function then prints the values of both the global variable (global_variable) and the local variable (local_variable). It demonstrates that you can access global and local variables within a function.
Part 3: Function call
- 1
example_function()
In this part, you call the example_function() by invoking it. This results in the function's code being executed.
As a result of this function call, it will print the values of the global and local variables within the function.
Part 4: Accessing global variable outside the function
- 1
print(global_variable) # Accessible outside the function
After calling the function, you print the value of the global variable global_variable outside the function. This demonstrates that global variables are accessible inside and outside of functions.
Part 5: Attempting to access local variable outside the function
- 1
# print(local_variable) # Error, local variable not visible here
In this part, you are attempting to print the value of the local variable local_variable outside of the function. However, this line would result in an error.
Local variables are only visible and accessible within the scope of the function where they are defined.
Attempting to access them outside of that scope would raise a "NameError".
Using functions with loops
Functions and loops together
- Functions can contain code with loops
- This makes complex tasks more organized
- The loop code becomes a repeatable function
Example:
- 1
- 2
- 3
- 4
- 5
def print_numbers(limit):for i in range(1, limit+1):print(i)print_numbers(5) # Output: 1 2 3 4 5
Enhancing code organization and reusability
- Functions group similar actions for easy understanding
- Looping within functions keeps code clean
- You can reuse a function to repeat actions
Example
- 1
- 2
- 3
- 4
- 5
def greet(name):return "Hello, " + namefor _ in range(3):print(greet("Alice"))
Modifying data structure using functions
You'll use Python and a list as the data structure for this illustration. In this example, you will create functions to add and remove elements from a list.
Part 1: Initialize an empty list
- 1
- 2
# Define an empty list as the initial data structuremy_list = []
In this part, you start by creating an empty list named my_list. This empty list serves as the data structure that you will modify throughout the code.
Part 2: Define a function to add elements
- 1
- 2
- 3
# Function to add an element to the listdef add_element(data_structure, element):data_structure.append(element)
Here, you define a function called add_element. This function takes two parameters:
data_structure: This parameter represents the list to which you want to add an elementelement: This parameter represents the element you want to add to the list
Inside the function, you use the append method to add the provided element to the data_structure, which is assumed to be a list.
Part 3: Define a function to remove elements
- 1
- 2
- 3
- 4
- 5
- 6
# Function to remove an element from the listdef remove_element(data_structure, element):if element in data_structure:data_structure.remove(element)else:print(f"{element} not found in the list.")
In this part, you define another function called remove_element. It also takes two parameters:
data_structure: The list from which we want to remove an elementelement: The element we want to remove from the list
Inside
the function, you use conditional statements to check if the element is
present in the data_structure. If it is, you use the remove
method to remove the first occurrence of the element. If it's not
found, you print a message indicating that the element was not found in
the list.
Part 4: Add elements to the list
- 1
- 2
- 3
- 4
# Add elements to the list using the add_element functionadd_element(my_list, 42)add_element(my_list, 17)add_element(my_list, 99)
Here, you use the add_element function to add three elements (42, 17, and 99) to the my_list. These are added one at a time using function calls.
Part 5: Print the current list
- 1
- 2
# Print the current listprint("Current list:", my_list)
This part simply prints the current state of the my_list to the console, allowing us to see the elements that have been added so far.
Part 6: Remove elements from the list
- 1
- 2
- 3
# Remove an element from the list using the remove_element functionremove_element(my_list, 17)remove_element(my_list, 55) # This will print a message since 55 is not in the list
In this part, you use the remove_element
function to remove elements from the my_list. First, you attempt to
remove 17 (which is in the list), and then you try to remove 55 (which
is not in the list). The second call to remove_element will print a message indicating that 55 was not found.
Part 7: Print the updated list
- 1
- 2
# Print the updated listprint("Updated list:", my_list)
Finally, you print the updated my_list to
the console. This allows us to observe the modifications made to the
list by adding and removing elements using the defined functions.
Conclusion
Congratulations! You've completed the Reading Instruction Lab on Python functions. You've gained a solid understanding of functions, their significance, and how to create and use them effectively. These skills will empower you to write more organized, modular, and powerful code in your Python projects.
Exception Handling in Python
Estimated time needed: 10 Minutes
Objectives
- Understanding Exceptions
- Distinguishing Errors from Exceptions
- Familiarity with Common Python Exceptions
- Managing Exceptions Effectively
What are exceptions?
Exceptions are alerts when something unexpected happens while running a program. It could be a mistake in the code or a situation that was not planned for. Python can raise these alerts automatically, but we can also trigger them on purpose using the raise command. The cool part is that we can prevent our program from crashing by handling exceptions.
Errors vs. Exceptions
Hold on, what is the difference between errors and exceptions? Well, errors
are usually big problems that come from the computer or the system.
They often make the program stop working completely. On the other hand, exceptions
are more like issues we can control. They happen because of something
we did in our code and can usually be fixed, so the program keeps going.
Here is the difference between Errors and exceptions:-
| Aspect | Errors | Exceptions |
|---|---|---|
| Origin | Errors are typically caused by the environment, hardware, or operating system. | Exceptions are usually a result of problematic code execution within the program. |
| Nature | Errors are often severe and can lead to program crashes or abnormal termination. | Exceptions are generally less severe and can be caught and handled to prevent program termination. |
| Handling | Errors are not usually caught or handled by the program itself. | Exceptions can be caught using try-except blocks and dealt with gracefully, allowing the program to continue execution. |
| Examples | Examples include “SyntaxError” due to incorrect syntax or “NameError” when a variable is not defined. | Examples include “ZeroDivisionError” when dividing by zero, or “FileNotFoundError” when attempting to open a non-existent file. |
| Categorization | Errors are not classified into categories. | Exceptions are categorized into various classes, such as “ArithmeticError,” “IOError,” ValueError,” etc., based on their nature. |
Common Exceptions in Python
Here are a few examples of exceptions we often run into and can handle using this tool:
- ZeroDivisionError: This error arises when an
attempt is made to divide a number by zero. Division by zero is
undefined in mathematics, causing an arithmetic error. For instance:
For example:
- 1
- 2
- 3
result = 10 / 0print(result)# Raises ZeroDivisionError
- ValueError: This error occurs when an inappropriate
value is used within the code. An example of this is when trying to
convert a non-numeric string to an integer:
For example:
- 1
- 2
num = int("abc")# Raises ValueError
- FileNotFoundError: This exception is encountered when an attempt is made to access a file that does not exist.
For example:
- 1
- 2
with open("nonexistent_file.txt", "r") as file:content = file.read() # Raises FileNotFoundError
- IndexError: An IndexError occurs when an index is used to access an element in a list that is outside the valid index range.
For example:
- 1
- 2
- 3
my_list = [1, 2, 3]value = my_list[1] # No IndexError, within rangemissing = my_list[5] # Raises IndexError
- KeyError: The KeyError arises when an attempt is made to access a non-existent key in a dictionary.
For example:
- 1
- 2
- 3
my_dict = {"name": "Alice", "age": 30}value = my_dict.get("city") # No KeyError, using .get() methodmissing = my_dict["city"] # Raises KeyError
- TypeError: The TypeError occurs when an object is
used in an incompatible manner. An example includes trying to
concatenate a string and an integer:
For example:
- 1
- 2
result = "hello" + 5# Raises TypeError
- AttributeError: An AttributeError occurs when an
attribute or method is accessed on an object that doesn't possess that
specific attribute or method. For instance:
For example:
- 1
- 2
- 3
text = "example"length = len(text) # No AttributeError, correct method usagemissing = text.some_method() # Raises AttributeError
- ImportError: This error is encountered when an attempt is made to import a module that is unavailable. For example:
import non_existent_module
Note: Please remember, the exceptions you will encounter are not limited to just these. There are many more in Python. However, there is no need to worry. By using the technique provided below and following the correct syntax, you will be able to handle any exceptions that come your way.
Handling Exceptions:
Python has a handy tool called try and except that helps us manage exceptions.
Try and Except : You can use the try and except blocks to prevent your program from crashing due to exceptions.
Here's how they work:
- The code that may result in an exception is contained in the try block.
- If an exception occurs, the code directly jumps to except block.
- In the except block, you can define how to handle the exception gracefully, like displaying an error message or taking alternative actions.
- After the except block, the program continues executing the remaining code.
Example: Attempting to divide by zero
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
# using Try- excepttry:# Attempting to divide 10 by 0result = 10 / 0except ZeroDivisionError:# Handling the ZeroDivisionError and printing an error messageprint("Error: Cannot divide by zero")# This line will be executed regardless of whether an exception occurredprint("outside of try and except block")
Next Step
As we finish up this reading, you are ready to move on to the next part where you will practice handling errors. For better learning, try out different types of data in the lab. This way, you will encounter various errors and learn how to deal with them effectively. This knowledge will help you write stronger and more reliable code in the future.
Author(s)

Python Objects and Classes
Estimated time needed: 10 minutes
Objectives
In this reading, you will learn about:
- Fundamental concepts of Python objects and classes.
- Structure of classes and object code.
- Real-world examples related to objects and classes.
Introduction to classes and object
Python is an object-oriented programming (OOP) language that uses a paradigm centered around objects and classes.
Let's look at these fundamental concepts.
Classes
A class is a blueprint or template for creating objects. It defines the structure and behavior that its objects will have.
Think of a class as a cookie cutter and objects as the cookies cut from that template.
In Python, you can create classes using the class keyword.
Creating classes
When you create a class, you specify the attributes(data) and methods (functions) that objects of that class will have.Attributes are defined as variables within the class, and methods are defined as functions.
For example,you can design a "Car" class with attributes such as "color" and "speed," along with methods like "accelerate."
Objects
An object is a fundamental unit in Python that represents a real-world entity or concept.
Objects can be tangible (like a car) or abstract (like a student's grade).
Every object has two main characteristics:
State
The attributes or data that describe the object. For your "Car" object, this might include attributes like "color", "speed", and "fuel level".
Behavior
The actions or methods that the object can perform. In Python, methods are functions that belong to objects and can change the object's state or perform specific operations.
Instantiating objects
- Once you've defined a class, you can create individual objects (instances) based on that class.
- Each object is independent and has its own set of attributes and methods.
- To create an object, you use the class name followed by parentheses, so: "my_car = Car()"
Interacting with objects
You interact with objects by calling their methods or accessing their attributes using dot notation.
For example, if you have a Car object named my_car, you can set its color with my_car.color = "blue" and accelerate it with my_car.accelerate() if there's an accelerate method defined in the class.
Structure of classes and object code
Please don't directly copy and use this code because it is a template for explanation and not for specific results.
Class declaration (class ClassName)
- The
classkeyword is used to declare a class in Python. ClassNameis the name of the class, typically following CamelCase naming conventions.
- 1
class ClassName:
Class attributes (class_attribute = value)
- Class attributes are variables shared among all class instances (objects).
- They are defined within the class but outside of any methods.
- 1
- 2
- 3
class ClassName:# Class attributes (shared by all instances)class_attribute = value
Constructor method (def init(self, attribute1, attribute2, …):)
- The
__init__method is a special method known as the constructor. - It initializes the instance attributes (also called instance variables) when an object is created.
- The
selfparameter is the first parameter of the constructor, referring to the instance being created. - attribute1, attribute2, and so on are parameters passed to the constructor when creating an object.
- Inside the constructor,
self.attribute1,self.attribute2, and so on are used to assign values to instance attributes.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
class ClassName:# Class attributes (shared by all instances)class_attribute = value# Constructor method (initialize instance attributes)def __init__(self, attribute1, attribute2, ...):pass# ...
Instance attributes (self.attribute1 = attribute1)
- Instance attributes are variables that store data specific to each class instance.
- They are initialized within the
__init__method using the self keyword followed by the attribute name. - These attributes hold unique data for each object created from the class.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
class ClassName:# Class attributes (shared by all instances)class_attribute = value# Constructor method (initialize instance attributes)def __init__(self, attribute1, attribute2, ...):self.attribute1 = attribute1self.attribute2 = attribute2# ...
Instance methods (def method1(self, parameter1, parameter2, …):)
- Instance methods are functions defined within the class.
- They operate on the instance's data (instance attributes) and can perform actions specific to instances.
- The self parameter is required in instance methods, allowing them to access instance attributes and call other methods within the class.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
class ClassName:# Class attributes (shared by all instances)class_attribute = value# Constructor method (initialize instance attributes)def __init__(self, attribute1, attribute2, ...):self.attribute1 = attribute1self.attribute2 = attribute2# ...# Instance methods (functions)def method1(self, parameter1, parameter2, ...):# Method logicpass
Using the same steps you can define multiple instance methods.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
class ClassName:# Class attributes (shared by all instances)class_attribute = value# Constructor method (initialize instance attributes)def __init__(self, attribute1, attribute2, ...):self.attribute1 = attribute1self.attribute2 = attribute2# ...# Instance methods (functions)def method1(self, parameter1, parameter2, ...):# Method logicpassdef method2(self, parameter1, parameter2, ...):# Method logicpass
Note: Now, you have successfully created a dummy class.
Creating objects (Instances)
- To create objects (instances) of the class, you call the class like a function and provide arguments the constructor requires.
- Each object is a distinct instance of the class, with its own instance attributes and the ability to call methods defined in the class.
- 1
- 2
- 3
# Create objects (instances) of the classobject1 = ClassName(arg1, arg2, ...)object2 = ClassName(arg1, arg2, ...)
Calling methods on objects
- In this section, you will call methods on objects, specifically
object1andobject2. - The methods method1 and method2 are defined in the ClassName class, and you're calling them on object1 and object2 respectively.
- You pass values param1_value and param2_value as arguments to these methods. These arguments are used within the method's logic.
Method 1: Using dot notation
- This is the most straightforward way to call an object's method. In this, use the dot notation (object.method()) to invoke the method on the object directly.
- For example,
result1 = object1.method1(param1_value, param2_value, ...)calls method1 on object1.
- 1
- 2
- 3
- 4
# Calling methods on objects# Method 1: Using dot notationresult1 = object1.method1(param1_value, param2_value, ...)result2 = object2.method2(param1_value, param2_value, ...)
Method 2: Assigning object methods to variables
- Here's an alternative way to call an object's method by assigning the method reference to a variable.
method_reference = object1.method1assigns the method method1 of object1 to the variable method_reference.- Later, call the method using the variable like this: result3 = method_reference(param1_value, param2_value, …).
- 1
- 2
- 3
# Method 2: Assigning object methods to variablesmethod_reference = object1.method1 # Assign the method to a variableresult3 = method_reference(param1_value, param2_value, ...)
Accessing object attributes
- Here, you are accessing an object's attribute using dot notation.
attribute_value = object1.attribute1retrieves the value of the attribute attribute1 from object1 and assigns it to the variable attribute_value.
- 1
- 2
# Accessing object attributesattribute_value = object1.attribute1 # Access the attribute using dot notation
Modifying object attributes
- You will modify an object's attribute using dot notation.
object1.attribute2 = new_valuesets the attribute attribute2 of object1 to the new value new_value.
- 1
- 2
# Modifying object attributesobject1.attribute2 = new_value # Change the value of an attribute using dot notation
Accessing class attributes (shared by all instances)
- Finally, access a class attribute shared by all class instances.
class_attr_value = ClassName.class_attributeaccesses the class attributeclass_attributefrom the ClassNameclassand assigns its value to the variable.class_attr_value.
- 1
- 2
# Accessing class attributes (shared by all instances)class_attr_value = ClassName.class_attribute
Real-world example
Let's write a python program that simulates a simple car class, allowing you to create car instances, accelerate them, and display their current speeds.
- Let's start by defining a
Carclass that includes the following attributes and methods:
Class attribute
max_speed, which is set to 120 km/h.Constructor method
__init__that takes parameters for the car's make, model, color, and an optional speed (defaulting to 0). This method initializes instance attributes for make, model, color, and speed.Method
accelerate(self, acceleration)that allows the car to accelerate. If the acceleration does not exceed themax_speed, update the car's speed attribute. Otherwise, set the speed to the max_speed.Method
get_speed(self)that returns the current speed of the car.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
class Car:# Class attribute (shared by all instances)max_speed = 120 # Maximum speed in km/h# Constructor method (initialize instance attributes)def __init__(self, make, model, color, speed=0):self.make = makeself.model = modelself.color = colorself.speed = speed # Initial speed is set to 0# Method for accelerating the cardef accelerate(self, acceleration):if self.speed + acceleration <= Car.max_speed:self.speed += accelerationelse:self.speed = Car.max_speed# Method to get the current speed of the cardef get_speed(self):return self.speed
- Now, you will instantiate two objects of the
Carclass, each with the following characteristics:
car1: Make = "Toyota", Model = "Camry", Color = "Blue"
car2: Make = "Honda", Model = "Civic", Color = "Red"
- 1
- 2
- 3
# Create objects (instances) of the Car classcar1 = Car("Toyota", "Camry", "Blue")car2 = Car("Honda", "Civic", "Red")
- Using the
acceleratemethod, you will increase the speed of car1 by 30 km/h and car2 by 20 km/h.
- 1
- 2
- 3
# Accelerate the carscar1.accelerate(30)car2.accelerate(20)
- Lastly, you will display the current speed of each car by utilizing the
get_speedmethod.
- 1
- 2
- 3
# Print the current speeds of the carsprint(f"{car1.make} {car1.model} is currently at {car1.get_speed()} km/h.")print(f"{car2.make} {car2.model} is currently at {car2.get_speed()} km/h.")
Next steps
In conclusion, this reading provides a fundamental understanding of objects and classes in Python, essential concepts in object-oriented programming. Classes serve as blueprints for creating objects, encapsulating data attributes and methods. Objects represent real-world entities and possess their unique state and behavior. The structured code example presented in the reading outlines the key elements of a class, including class attributes, the constructor method for initializing instance attributes, and instance methods for defining object-specific functionality.
In the upcoming laboratory session, you can apply the concepts of objects and classes to gain hands-on experience.
Author

Python Programming Fundamentals Cheat Sheet
| Package/Method | Description | Syntax and Code Example |
|---|---|---|
| AND | Returns `True` if both statement1 and statement2 are `True`. Otherwise, returns `False`. | Syntax:
Example:
|
| Class Definition | Defines a blueprint for creating objects and defining their attributes and behaviors. | Syntax:
Example:
|
| Define Function | A `function` is a reusable block of code that performs a specific task or set of tasks when called. | Syntax:
Example:
|
| Equal(==) | Checks if two values are equal. | Syntax:
Example 1:
returns True Example 2:
returns False |
| For Loop | A `for` loop repeatedly executes a block of code for a specified number of iterations or over a sequence of elements (list, range, string, etc.). | Syntax:
Example 1:
Example 2:
|
| Function Call | A function call is the act of executing the code within the function using the provided arguments. | Syntax:
Example:
|
| Greater Than or Equal To(>=) | Checks if the value of variable1 is greater than or equal to variable2. | Syntax:
Example 1:
returns True Example 2:
returns True |
| Greater Than(>) | Checks if the value of variable1 is greater than variable2. | Syntax:
Example 1: 9 > 6 returns True Example 2:
returns False |
| If Statement | Executes code block `if` the condition is `True`. | Syntax:
Example:
|
| If-Elif-Else | Executes the first code block if condition1 is `True`, otherwise checks condition2, and so on. If no condition is `True`, the else block is executed. | Syntax:
Example:
|
| If-Else Statement | Executes the first code block if the condition is `True`, otherwise the second block. | Syntax:
Example:
|
| Less Than or Equal To(<=) | Checks if the value of variable1 is less than or equal to variable2. | Syntax:
Example 1:
returns True Example 2:
returns True |
| Less Than(<) | Checks if the value of variable1 is less than variable2. | Syntax:
Example 1:
returns True Example 2:
returns True |
| Loop Controls | `break` exits the loop prematurely. `continue` skips the rest of the current iteration and moves to the next iteration. | Syntax:
Example 1:
Example 2:
|
| NOT | Returns `True` if variable is `False`, and vice versa. | Syntax:
Example:
returns True if the variable is False (i.e., unlocked). |
| Not Equal(!=) | Checks if two values are not equal. | Syntax:
Example:
returns True Example 2:
returns False |
| Object Creation | Creates an instance of a class (object) using the class constructor. | Syntax:
Example:
|
| OR | Returns `True` if either statement1 or statement2 (or both) are `True`. Otherwise, returns `False`. | Syntax:
Example:
returns True |
| range() | Generates a sequence of numbers within a specified range. | Syntax:
Example:
|
| Return Statement | `Return` is a keyword used to send a value back from a function to its caller. | Syntax:
Example:
|
| Try-Except Block | Tries to execute the code in the try block. If an exception of the specified type occurs, the code in the except block is executed. | Syntax:
Example:
|
| Try-Except with Else Block | Code in the `else` block is executed if no exception occurs in the try block. | Syntax:
Example:
|
| Try-Except with Finally Block | Code in the `finally` block always executes, regardless of whether an exception occurred. | Syntax:
Example:
|
| While Loop | A `while` loop repeatedly executes a block of code as long as a specified condition remains `True`. | Syntax:
Example:
|
© IBM Corporation. All rights reserved.
Glossary: Python Programming Fundamentals
Welcome! This alphabetized glossary contains many of the terms you'll find within this course. This comprehensive glossary also includes additional industry-recognized terms not used in course videos. These terms are important for you to recognize when working in the industry, participating in user groups, and participating in other certificate programs.
| Term | Definition |
|---|---|
| Analogy | Refers to a concept or comparison outside the scope of the programming language itself, used to explain or relate one concept to another in a more understandable way. |
| Attributes | Attributes in Python refer to the characteristics or properties of an object, and they can be accessed using dot notation. |
| Branching | Branching in Python is a process of altering the flow of a program based on conditions, typically using if, elif, and else statements. |
| Comparison operators | Comparison operators in Python are used to compare values and return Boolean results (True or False), including operators like == (equal),!= (not equal), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to). |
| Conditions | Conditions in Python are used to make decisions in code, executing specific blocks of code based on whether a given expression evaluates to True or False. |
| Enumerate | In Python, "enumerate" is a built-in function that adds a counter to an iterable, allowing you to loop through both the elements and their corresponding indices. |
| Exception handling | Exception handling in Python is a mechanism for gracefully managing and responding to errors or exceptional conditions that may occur during program execution. |
| Explicitly | In Python, the term "explicitly" refers to performing an action or specifying something in a clear, unambiguous, and direct manner. |
| For loops | For loops in Python are used for iterating over a sequence (such as a list, tuple, or string) or other iterable objects, executing a set of statements for each item in the sequence. |
| Global variable | Global variables in Python are variables defined outside of any function or block and can be accessed and modified from any part of the code. |
| Incremented | "Incremented" in Python means to increase the value of a variable by a specified amount, typically done using the += operator or by adding a fixed value. |
| Indent | In Python, "indent" refers to the use of whitespace at the beginning of a line to signify the structure and scope of code blocks, such as loops and functions. |
| Indices | In Python, "indices" refer to the position or location of elements in a sequence, like a string, list, or tuple, starting with 0 for the first element. |
| Iterate | In Python, "iterate" means to repeatedly perform a set of operations or steps on each item in a collection, such as a list, tuple, or dictionary, typically using loops or iterators. |
| Local variables | Local variables in Python are variables defined within a specific function or block of code and are only accessible within that function or block. |
| Logic operators | Logic operators in Python are used to perform logical operations on Boolean values, including operators like and (logical AND), or (logical OR), and not (logical NOT). |
| Loops | Loops in Python are constructs for repeating a block of code, enabling the execution of the same code multiple times. |
| Parameters | Parameters in Python are placeholders in a function definition, used to accept and work with values provided to the function when it is called. |
| Programming Fundamentals | Programming fundamentals in Python involve variables, control structures, functions, data structures, input/output, and error handling for building software. |
| Range function | The range function in Python generates a sequence of numbers that can be used for iterating in a loop and is typically used as range (start, stop, step), where it creates numbers from start to stop-1 with the given step increment. |
| Scope of function | The "scope of a function" in Python refers to the region of code where a variable defined within that function is accessible or visible. |
| Sequences | Sequences in Python are ordered collections of items that can include data types like strings, lists, and tuples, allowing for indexing and iteration. |
| Syntax | In Python, "Syntax" refers to the set of rules that dictate how code must be written and structured to be correctly interpreted by the Python interpreter. It includes correct use of keywords, indentation, operators, and punctuation. |
| While loops | While loops in Python are used to repeatedly execute a block of code as long as a specified condition is true. |

Module 4¶
#print(ModuleFolder)
#lnk="/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork_labs_Cheat_Sheet_Week-1.md.html"
ModuleFolder='C:/Users/Gamaliel/Documents/G/ADD/IBM_DS/Python4DS/IBMs-NBs/Mod4/'
for file in os.listdir(ModuleFolder):
#if file.lower().endswith('HTMLs.html'):
# continue
if file.lower().endswith('.html'):
link=file
print(link)
display(HTML(ModuleFolder+link))
elif file.lower().endswith('.htm'):
link=file
print(link)
display(HTML(ModuleFolder+link))
else:
continue
#for file in os.listdir(ModuleFolder):
# if not file.lower().endswith('.htm'):
# continue
# link=file
#print(link)
#display(HTML(ModuleFolder+link))
01.open.htm
Reading a file with Open()
Estimated time needed: 10 minutes
File
handling is an essential aspect of programming, and Python provides
built-in functions to interact with files. This guide will explore how
to use Python's open function to read the text files ('.txt' files).
Objectives
- Describe how to use the open() and read() Python functions to open and read the contents of a text file
- Explain how to use the with statement in Python
- Describe how to use the readline() function in Python
- Explain how to use the seek() function to read specific character(s) in a text file
Introduction
Reading text files involves extracting and processing the data stored within them. Text files can have various structures, and how you read them depends on their format. Here's a general guide on reading text files with different structures.
Plain text files
- Plain text files contain unformatted text without any specific structure
- You can read plain text files line by line or load all the content into your memory
Opening the file
There are two methods for opening the file using the file handling concept.
1. Using Python's open function
Suppose we have a file named 'file.txt'.
Python's open function creates a file object and accesses the data within a text file. It takes two primary parameters:
File path: The file path parameter consists of the filename and directory where the file is located.
Mode: The mode parameter specifies the purpose of opening the file, such as 'r' for reading, 'w' for writing, or 'a' for appending.
- 1
- 2
# Open the file in read ('r') modefile = open('file.txt', 'r')
open('file.txt', 'r'):
This line
opens a file named 'file.txt' in read mode ('r'). It returns a file
object, which is stored in the variable file. The 'r' mode indicates
that the file will be opened for reading.
2. Using 'with' statement
To simplify file handling and ensure proper closure of files, Python provides the "with" statement. It automatically closes the file when operations within the indented block are completed. This is considered best practice when working with files.
- 1
- 2
- 3
# Open the file using 'with' in read ('r') modewith open('file.txt', 'r') as file:# further code
Open the file using 'with' in read ('r') mode
with open('file.txt', 'r') as file:
This
line opens a file named 'file.txt' in read mode ('r') using the with
statement, which is a context manager. The file is automatically closed
when the code block inside the with statement exits.
Advantages of using the with statement
The key advantages of using the 'with' statement are:
- Automatic resource management: The file is guaranteed to be closed when you exit the with block, even if an exception occurs during processing.
- Cleaner and more concise code: You don't need to explicitly call close(), making your code more readable and less error-prone.
Note: For most file reading and writing operations in Python, the 'with' statement is recommended.
Let's perform a read operation on a file
1. Reading the entire content
You can read the entire content of a file using the read method, which stores the data as a string in a variable. This content can be printed or further manipulated as needed.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
# Reading and Storing the Entire Content of a File# Using the read method, you can retrieve the complete content of a file# and store it as a string in a variable for further processing or display.# Step 1: Open the file you want to readwith open('file.txt', 'r') as file:# Step 2: Use the read method to read the entire content of the filefile_stuff = file.read()# Step 3: Now that the file content is stored in the variable 'file_stuff',# you can manipulate or display it as needed.# For example, let's print the content to the console:print(file_stuff)# Step 4: The 'with' statement automatically closes the file when it's done,# ensuring proper resource management and preventing resource leaks.
Step 1: Involves opening the file, specifying 'file.txt' as the file to be opened for reading ('r') mode using the with context manager.
Step 2: Utilizes the read() statement on the file object (file) to read the entire file. This content is then stored in the file_stuff variable.
Step 3: Explain that with the content now stored in file_stuff, you can perform various operations on it. In the example provided, the code prints the content to the console, but you can manipulate, analyze, search, or process the text data in file_stuff based on your specific needs.
Step 4: Emphasizes that the with block automatically closes the file when done, ensuring proper resource management and preventing resource leaks. This is a crucial aspect of using the with statement when working with files.
2. Reading the content line by line
Python provides methods to read files line by line:
The 'readlines' method reads the file line by line and stores each line as an element in a list. The order of lines in the list corresponds to their order in the file.
The 'readline' method reads individual lines from the file. It can be called multiple times to read subsequent lines.
In Python, the readline() method is like reading a book one line at a time. Imagine you have a big book and want to read it page by page. readline() helps you do just that with lines of text instead of pages.
Here's how it works:
Opening a file: First, you need to open the file you want to read using the open() function.
- 1
file = open('file.txt', 'r')
Reading line by line: Now, you can use readline() to read one line from the file at a time. It's like turning the pages of the book, but here, you're getting one sentence (or line) at each turn.
- 1
- 2
line1 = file.readline() # Reads the first lineline2 = file.readline() # Reads the second line
Using the lines: You can do things with each line you read. For example, you can print it, check if it contains specific words, or save it somewhere else.
- 1
- 2
- 3
print(line1) # Print the first lineif 'important' in line2:print('This line is important!')
Looping through lines: Typically, you use a loop to read lines until no more lines are left. t's like reading the entire book, line by line.
- 1
- 2
- 3
- 4
- 5
while True:line = file.readline()if not line:break # Stop when there are no more lines to readprint(line)
Closing the book: When you're done reading, it's essential to close the file using file.close() to make sure you're not wasting resources.
- 1
file.close()
So, In simple terms, readline() helps you read a text file line by line, allowing you to work with each line of text as you go. It's like taking one sentence at a time from a book and doing something with it before moving on to the next sentence. Don't forget to close the book when you're done!
3. Reading specific characters
You can specify the number of characters to read using the readlines method. For example, reading the first four characters, the next five, and so on.
Reading specific characters from a text file in Python involves opening the file, navigating to the desired position, and then reading the characters you need. Here's a detailed explanation of how to read specific characters from a file:
Open the File
First, you need to open the file you want to read. Use the open() function with the appropriate file path and mode. For reading, use 'r' mode.
- 1
file = open('file.txt', 'r')
Navigate to the intended position (Optional)
If you want to read characters from a specific position in the file, you can use the seek() method. This method moves the file pointer (like a cursor) to a particular position. The position is specified in bytes, so you'll need to know the byte offset of the characters you want to read.
- 1
file.seek(10) # Move to the 11th byte (0-based index)
Read specific characters
To read specific characters, you can use the read() method with an argument that specifies the number of characters to read. It reads characters starting from the current position of the file pointer.
- 1
characters = file.read(5) # Read the next 5 characters
In this example, it reads the next 5 characters from the current position of the file pointer.
Use the read characters
You can now use the characters variable to work with the specific characters you've read. You can print them, save them, manipulate them, or perform any other actions.
- 1
print(characters)
Close the file
It's essential to close the file when you're done to free up system resources and ensure proper file handling.
- 1
file.close()
Conclusion
In conclusion, this reading has provided a comprehensive overview of file handling in Python, with a focus on reading text files. File handling is a fundamental aspect of programming, and Python offers powerful built-in functions and methods to interact with files seamlessly.
Author(s)
02.Writing.htm
Writing on a file with Open()
Estimated time needed: 10 minutes
Objective
- Create and write data to a file in Python
- Write multiple lines of text to a file using lists and loops
- Add new information to an already existing file without erasing its content
- Compare and contrast the different file modes in Python, what they mean, and when to use them
Writing to a file
You can create a new text file and write data to it using Python's open() function. The open()
function takes two main arguments: the file path (including the file
name) and the mode parameter, which specifies the operation you want to
perform on the file. For writing, you should use the mode 'w' Here's an
example:
- 1
- 2
- 3
- 4
- 5
# Create a new file Example2.txt for writingwith open('Example2.txt', 'w') as file1:file1.write("This is line A\n")file1.write("This is line B\n")# file1 is automatically closed when the 'with' block exits
Line 2 explanation:** with open('Example2.txt', 'w') as file1:
- We start by using the open function to open or create a file named
Example2.txtfor writing ('w' mode). - The
'w'mode specifies that we intend to write data to the file. - We use the
withstatement to ensure that the file is automatically closed when the code block exits. This helps manage resources efficiently.
Line 3 explanation: file1.write("This is line A\n")
- Here, we use the
write()method on the file object,file1, to add the textThis is line Ato the file. - The
\nat the end represents a newline character, which starts a new line in the file.
Line 4 explanation file1.write("This is line" B\n")
- Similarly, we use the
write()method again to add the textThis is line Bto the file on a new line.
Writing multiple lines to a file using a list and loop
In Python, you can use a list to store multiple lines of text and then write these lines to a file using a loop. Here's an example code snippet that demonstrates this:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
# List of lines to write to the fileLines = ["This is line 1", "This is line 2", "This is line 3"]# Create a new file Example3.txt for writingwith open('Example3.txt', 'w') as file2:for line in Lines:file2.write(line + "\n")# file2 is automatically closed when the 'with' block exits
Here's an explanation of the code:
Line 2: We start by defining a list called
Lines, which contains multiple lines of text that we want to write to the file. Each line is a string.Line 5: Next, we use the
open()function to create a new text file namedExample3.txtfor writing,'w'mode. The'w'mode indicates that we intend to write data to the file.Line 6: We then enter a for loop to iterate through each element (line) in the
Lineslist.Line 7: Inside the loop, we use the
write()method on the file objectfile2to write the current line of text (line) to the file. We add\nat the end of each line to ensure that each line is followed by a newline character, which separates them in the file.Line 8: Finally, we add a comment indicating that the file
file2will be automatically closed when the code block within the with statement exits. Properly closing the file is essential for good resource management.
Appending data to an existing file
In Python, you can use the 'a'
mode when opening a file to append new data to an existing file without
overwriting its contents. Here's an example code snippet that
demonstrates this:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
# Data to append to the existing filenew_data = "This is line C"# Open an existing file Example2.txt for appendingwith open('Example2.txt', 'a') as file1:file1.write(new_data + "\n")# file1 is automatically closed when the 'with' block exits
Here's an explanation of the code:
Line 2: We start by defining a variable
new_datathat contains the text we want to append to the existing file. In this case, it's the string `This is line C.``Line 5: Next, we use the
open()function to open an existing file namedExample2.txtfor appending,'a'mode. The'a'mode indicates that we intend to append data to the file, and if the file doesn't exist, it will be created.Line 6: Within the with block, we use the
write()method on the file objectfile1to append thenew_datato the file. We add"\n"at the end to ensure that the appended data starts on a new line, maintaining the file's readability.Finally, we add a comment indicating that the file
file1will automatically close when the code block within thewithstatement exits. Properly closing the file is essential for good resource management.
Copying contents from one file to another
In Python, you can copy the contents of one file to another by reading from the source file and writing to the destination file. Here's an example code snippet that demonstrates this:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
# Open the source file for readingwith open('source.txt', 'r') as source_file:# Open the destination file for writingwith open('destination.txt', 'w') as destination_file:# Read lines from the source file and copy them to the destination filefor line in source_file:destination_file.write(line)# Destination file is automatically closed when the 'with' block exits# Source file is automatically closed when the 'with' block exits
Here's an explanation of the code:
Line 2: We start by opening the source file,
source.txtfor reading,rmode, using thewithstatement and theopen()function. This allows us to read data from the source file.Line 4: Inside the first with block, we open the destination file,
destination.txtfor writing,wmode, using anotherwithstatement and theopen()function. This prepares the destination file for writing.Line 6: We use a
forloop to iterate through each line in the source filesource_file. This loop reads each line from the source file one by one.Line 7: Within the loop, we use the
write()method to write each line from the source file to the destination filedestination_file. This effectively copies the content of the source file to the destination file.Lines 8 and 9: After copying all the lines, both the source and destination files are automatically closed when their respective with blocks exit. Proper file closure is crucial for managing resources efficiently.
File modes in Python (syntax and use cases)
The following table provides an overview of different file modes, their syntax, and common use cases. Understanding these modes is essential when working with files in Python for various data manipulation tasks.
| Mode | Syntax | Description |
|---|---|---|
| ‘r’ | 'r' |
Read mode. Opens an existing file for reading. Raises an error if the file doesn't exist. |
| ‘w’ | 'w' |
Write mode. Creates a new file for writing. Overwrites the file if it already exists. |
| ‘a’ | 'a' |
Append mode. Opens a file for appending data. Creates the file if it doesn't exist. |
| ‘x’ | 'x' |
Exclusive creation mode. Creates a new file for writing but raises an error if the file already exists. |
| ‘rb’ | 'rb' |
Read binary mode. Opens an existing binary file for reading. |
| ‘wb’ | 'wb' |
Write binary mode. Creates a new binary file for writing. |
| ‘ab’ | 'ab' |
Append binary mode. Opens a binary file for appending data. |
| ‘xb’ | 'xb' |
Exclusive binary creation mode. Creates a new binary file for writing but raises an error if it already exists. |
| ‘rt’ | 'rt' |
Read text mode. Opens an existing text file for reading. (Default for text files) |
| ‘wt’ | 'wt' |
Write text mode. Creates a new text file for writing. (Default for text files) |
| ‘at’ | 'at' |
Append text mode. Opens a text file for appending data. (Default for text files) |
| ‘xt’ | 'xt' |
Exclusive text creation mode. Creates a new text file for writing but raises an error if it already exists. |
| ‘r+’ | 'r+' |
Read and write mode. Opens an existing file for both reading and writing. |
| ‘w+’ | 'w+' |
Write and read mode. Creates a new file for reading and writing. Overwrites the file if it already exists. |
| ‘a+’ | 'a+' |
Append and read mode. Opens a file for both appending and reading. Creates the file if it doesn't exist. |
| ‘x+’ | 'x+' |
Exclusive creation and read/write mode. Creates a new file for reading and writing but raises an error if it already exists. |
Conclusion
Working with files is a fundamental aspect of programming, and Python provides powerful tools to perform various file operations. In this summary, we covered key concepts and code examples related to file handling in Python, including writing, appending, and copying files.
Author(s)
03.OpenPandas.htm
Introduction to Pandas for Data Analysis
Estimated time: 10 Mins
Objective:
- Learn what Pandas Series are and how to create them.
- Understand how to access and manipulate data within a Series.
- Discover the basics of creating and working with Pandas DataFrames.
- Learn how to access, modify, and analyze data in DataFrames.
- Gain insights into common DataFrame attributes and methods.
What is Pandas?
Pandas
is a popular open-source data manipulation and analysis library for the
Python programming language. It provides a powerful and flexible set of
tools for working with structured data, making it a fundamental tool
for data scientists, analysts, and engineers.
Pandas is designed to
handle data in various formats, such as tabular data, time series data,
and more, making it an essential part of the data processing workflow in
many industries.
Here are some key features and functionalities of Pandas:
Data Structures: Pandas offers two primary data structures - DataFrame and Series.
- A DataFrame is a two-dimensional, size-mutable, and potentially heterogeneous tabular data structure with labeled axes (rows and columns).
- A Series is a one-dimensional labeled array, essentially a single column or row of data.
Data Import and Export: Pandas makes it easy to read data from various sources, including CSV files, Excel spreadsheets, SQL databases, and more. It can also export data to these formats, enabling seamless data exchange.
Data Merging and Joining: You can combine multiple DataFrames using methods like merge and join, similar to SQL operations, to create more complex datasets from different sources.
Efficient Indexing: Pandas provides efficient indexing and selection methods, allowing you to access specific rows and columns of data quickly.
Custom Data Structures: You can create custom data structures and manipulate data in ways that suit your specific needs, extending Pandas' capabilities.
Importing Pandas:
Import Pandas using the import command, followed by the library's name.
Commonly, Pandas is imported as pd for brevity in code.
- 1
import pandas as pd
Data Loading:
- Pandas can be used to load data from various sources, such as CSV and Excel files.
- The read_csv function is used to load data from a CSV file into a Pandas DataFrame.
To read a CSV (Comma-Separated Values) file in Python using the Pandas library, you can use the pd.read_csv() function. Here's the syntax to read a CSV file:
- 1
- 2
- 3
- 4
import pandas as pd# Read the CSV file into a DataFramedf = pd.read_csv('your_file.csv')
Replace 'your_file.csv' with the actual file path of your CSV file. Make sure that the file is located in the same directory as your Python script, or you provide the correct file path.
What is a Series?
A
Series is a one-dimensional labeled array in Pandas. It can be thought
of as a single column of data with labels or indices for each element.
You can create a Series from various data sources, such as lists, NumPy
arrays, or dictionaries
Here's a basic example of creating a Series in Pandas:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
import pandas as pd# Create a Series from a listdata = [10, 20, 30, 40, 50]s = pd.Series(data)print(s)
In this example, we've created a Series named s with numeric data. Notice that Pandas automatically assigned numerical indices (0, 1, 2, 3, 4) to each element, but you can also specify custom labels if needed.
Accessing Elements in a Series
You can access elements in a Series using the index labels or integer positions. Here are a few common methods for accessing Series data:
Accessing by label
- 1
print(s[2]) # Access the element with label 2 (value 30)
Accessing by position
- 1
print(s.iloc[3]) # Access the element at position 3 (value 40)
Accessing multiple elements
- 1
print(s[1:4]) # Access a range of elements by label
Series Attributes and Methods
Pandas Series come with various attributes and methods to help you manipulate and analyze data effectively. Here are a few essential ones:
- values: Returns the Series data as a NumPy array.
- index: Returns the index (labels) of the Series.
- shape: Returns a tuple representing the dimensions of the Series.
- size: Returns the number of elements in the Series.
- mean(), sum(), min(), max(): Calculate summary statistics of the data.
- unique(), nunique(): Get unique values or the number of unique values.
- sort_values(), sort_index(): Sort the Series by values or index labels.
- isnull(), notnull(): Check for missing (NaN) or non-missing values.
- apply(): Apply a custom function to each element of the Series.
What is a DataFrames?
A DataFrame is a two-dimensional labeled data structure with columns of potentially different data types. Think of it as a table where each column represents a variable, and each row represents an observation or data point. DataFrames are suitable for a wide range of data, including structured data from CSV files, Excel spreadsheets, SQL databases, and more.
Creating DataFrames from Dictionaries:
DataFrames can be created from dictionaries, with keys as column labels and values as lists representing rows.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
import pandas as pd# Creating a DataFrame from a dictionarydata = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],'Age': [25, 30, 35, 28],'City': ['New York', 'San Francisco', 'Los Angeles', 'Chicago']}df = pd.DataFrame(data)print(df)
Column Selection:
You can select a single column from a DataFrame by specifying the column name within double brackets.
Multiple columns can be selected in a similar manner, creating a new DataFrame.
- 1
print(df['Name']) # Access the 'Name' column
Accessing Rows:
You can access rows by their index using .iloc[] or by label using .loc[].
- 1
- 2
print(df.iloc[2]) # Access the third row by positionprint(df.loc[1]) # Access the second row by label
Slicing:
You can slice DataFrames to select specific rows and columns.
- 1
- 2
print(df[['Name', 'Age']]) # Select specific columnsprint(df[1:3]) # Select specific rows
Finding Unique Elements:
Use the unique method to determine the unique elements in a column of a DataFrame.
- 1
unique_dates = df['Age'].unique()
Conditional Filtering:
You can filter data in a DataFrame based on conditions using inequality operators.
For instance, you can filter albums released after a certain year.
- 1
high_above_102 = df[df['Age'] > 25]
Saving DataFrames:
To save a DataFrame to a CSV file, use the to_csv method and specify the filename with a “.csv” extension.Pandas provides other functions for saving DataFrames in different formats.
- 1
df.to_csv('trading_data.csv', index=False)
DataFrame Attributes and Methods
DataFrames provide numerous attributes and methods for data manipulation and analysis, including:
- shape: Returns the dimensions (number of rows and columns) of the DataFrame.
- info(): Provides a summary of the DataFrame, including data types and non-null counts.
- describe(): Generates summary statistics for numerical columns.
- head(), tail(): Displays the first or last n rows of the DataFrame.
- mean(), sum(), min(), max(): Calculate summary statistics for columns.
- sort_values(): Sort the DataFrame by one or more columns.
- groupby(): Group data based on specific columns for aggregation.
- fillna(), drop(), rename(): Handle missing values, drop columns, or rename columns.
- apply(): Apply a function to each element, row, or column of the DataFrame.
Pandas offers a wide range of methods beyond these examples. For more detailed information, please refer to the official documentation available on the Pandas official website.
Conclusion
In
conclusion, mastering the use of Pandas Series and DataFrames is
essential for effective data manipulation and analysis in Python. Series
provide a foundation for handling one-dimensional data with labels,
while DataFrames offer a versatile, table-like structure for working
with two-dimensional data. Whether you're cleaning, exploring,
transforming, or analyzing data, these Pandas data structures, along
with their attributes and methods, empower you to efficiently and
flexibly manipulate data to derive valuable insights. By incorporating
Series and DataFrames into your data science toolkit, you'll be
well-prepared to tackle a wide range of data-related tasks and enhance
your data analysis capabilities.
To further your skills in data analysis with Pandas, consider the following next steps:
Practice:
Work with real datasets to apply what you've learned and gain hands-on experience.
Explore Documentation:
Visit the Pandas official website to explore the extensive documentation and discover more functions and methods.
Author
Changelog
| Date | Version | Changed by | Change Description |
|---|---|---|---|
| 2023-10-02 | 1.0 | Akansha Yadav | Created Reading |
04.Matrices.htm
Reading: Matrix Mathematics
Estimated effort: 5 mins
You have seen that you can use Numpy package functions to perform different types of operations on arrays and matrices. In this reading, you will learn how these operations work mathematically.
1D Arrays : Vectors
A
1D array is often termed as a vector. Depending upon the orientation of
the data, the vector can be classified as a row vector or a column
vector. This is illustrated in the image below.
Mathematically, we can add, subtract, and take the product of two vectors, provided they are the same shape. The images below highlight the mathematical operations conducted on a pair of vectors.


All three of these operations are conducted on corresponding elements of individual vectors. The resulting array always has the same size as that of the two original vectors.
To a single vector, we can also add a constant (scalar addition), subtract a constant (scalar subtraction) and multiply a constant (scalar multiplication) to any vector. The images below illustrate these operations.


2D Arrays : Matrices
A 2D array is also called a Matrix. These are typically rectangular arrays with data stored in different rows. All of the operations mentioned above are also applicable to the 2D arrays. However, the Dot product of 2D matrices follows a different rule.
As illlustrated in the images below, the dot product is carried out by multiplying and adding corresponding elements of rows of the first matrix with the elements of columns of the second matrix. As a result, the output matrix from the multiplication will have a modified shape.
The general rule is that the dot product of an m X n matrix can be done only with an n X p matrix, and the resultant matrix will have the shape m X p. In the example shown below, the 4 X 2 matrix is multiplied with the 2 X 4 matrix to generate a 4 X 4 matrix.

In the reverse example, when 2 X 4 matrix is multiplied with the 4 X 2 one, the resultant will be a 2 X 2 matrix.

Note: Dot product of a row vector with a column vector, with the same number of elements, would return a single scalar value. Dot product of a column vector with a row vector, will return a 2D matrix.
Author: Abhishek Gagneja
05.BeginnersNumpy.htm
Beginner's Guide to NumPy
Estimated Time : 10 Minutes
Objective:
In this reading, you'll learn:
- Basics of NumPy
- How to create NumPy arrays
- Array attributes and indexing
- Basic operations like addition and multiplication
What is NumPy?
NumPy, short for Numerical Python, is a fundamental library for numerical and scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of high-level mathematical functions to operate on these arrays. NumPy serves as the foundation for many data science and machine learning libraries, making it an essential tool for data analysis and scientific research in Python.
Key aspects of NumPy in Python:
Efficient data structures: NumPy introduces efficient array structures, which are faster and more memory-efficient than Python lists. This is crucial for handling large data sets.
Multi-dimensional arrays: NumPy allows you to work with multi-dimensional arrays, enabling the representation of matrices and tensors. This is particularly useful in scientific computing.
Element-wise operations: NumPy simplifies element-wise mathematical operations on arrays, making it easy to perform calculations on entire data sets in one go.
Random number generation: It provides a wide range of functions for generating random numbers and random data, which is useful for simulations and statistical analysis.
Integration with other libraries: NumPy seamlessly integrates with other data science libraries like SciPy, Pandas, and Matplotlib, enhancing its utility in various domains.
Performance optimization: NumPy functions are implemented in low-level languages like C and Fortran, which significantly boosts their performance. It's a go-to choice when speed is essential.
Installation
If you haven't already installed NumPy, you can do so using pip:
- 1
pip install numpy
Creating NumPy arrays
You can create NumPy arrays from Python lists. These arrays can be one-dimensional or multi-dimensional.
Creating 1D array
- 1
import numpy as np
import numpy as np: In this line, the NumPy library is imported and assigned an alias np to make it easier to reference in the code.
- 1
- 2
# Creating a 1D arrayarr_1d = np.array([1, 2, 3, 4, 5]) # **np.array()** is used to create NumPy arrays.
arr_1d = np.array([1, 2, 3, 4, 5]): In this line, a one-dimensional NumPy array named arr_1d is created. It uses the np.array()
function to convert a Python list [1, 2, 3, 4, 5] into a NumPy array.
This array contains five elements, which are 1, 2, 3, 4, and 5. arr_1d is a 1D array because it has a single row of elements.
Creating 2D array
- 1
import numpy as np
import numpy as np: In this line, the NumPy library is imported and assigned an alias np to make it easier to reference in the code.
- 1
- 2
# Creating a 2D arrayarr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]): In this line, a two-dimensional NumPy array named arr_2d is created. It uses the np.array() function to convert a list of lists into a 2D NumPy array.
The outer list contains three inner lists, each of which represents a row of elements. So, arr_2d
is a 2D array with three rows and three columns. The elements in this
array form a matrix with values from 1 to 9, organized in a 3x3 grid.
Array attributes
NumPy arrays have several useful attributes:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
# Array attributesprint(arr_2d.ndim) # ndim : Represents the number of dimensions or "rank" of the array.# output : 2print(arr_2d.shape) # shape : Returns a tuple indicating the number of rows and columns in the array.# Output : (3, 3)print(arr_2d.size) # size: Provides the total number of elements in the array.# Output : 9
Indexing and slicing
You can access elements of a NumPy array using indexing and slicing:
In this line, the third element (index 2) of the 1D array arr_1d is accessed.
- 1
- 2
# Indexing and slicingprint(arr_1d[2]) # Accessing an element (3rd element)
In this line, the element in the 2nd row (index 1) and 3rd column (index 2) of the 2D array arr_2d is accessed.
- 1
print(arr_2d[1, 2]) # Accessing an element (2nd row, 3rd column)
In this line, the 2nd row (index 1) of the 2D array arr_2d is accessed.
- 1
print(arr_2d[1]) # Accessing a row (2nd row)
In this line, the 2nd column (index 1) of the 2D array arr_2d is accessed.
- 1
print(arr_2d[:, 1]) # Accessing a column (2nd column)
Basic operations
NumPy simplifies basic operations on arrays:
Element-wise arithmetic operations:
Addition, subtraction, multiplication, and division of arrays with scalars or other arrays.
Array addition
- 1
- 2
- 3
- 4
- 5
# Array additionarray1 = np.array([1, 2, 3])array2 = np.array([4, 5, 6])result = array1 + array2print(result) # [5 7 9]
Scalar multiplication
- 1
- 2
- 3
- 4
# Scalar multiplicationarray = np.array([1, 2, 3])result = array * 2 # each element of an array is multiplied by 2print(result) # [2 4 6]
Element-wise multiplication (Hadamard Product)
- 1
- 2
- 3
- 4
- 5
# Element-wise multiplication (Hadamard product)array1 = np.array([1, 2, 3])array2 = np.array([4, 5, 6])result = array1 * array2print(result) # [4 10 18]
Matrix multiplication
- 1
- 2
- 3
- 4
- 5
- 6
- 7
# Matrix multiplicationmatrix1 = np.array([[1, 2], [3, 4]])matrix2 = np.array([[5, 6], [7, 8]])result = np.dot(matrix1, matrix2)print(result)# [[19 22]# [43 50]]
NumPy simplifies these operations, making it easier and more efficient than traditional Python lists.
Operation with NumPy
Here's the list of operation which can be performed using Numpy
| Operation | Description | Example |
|---|---|---|
| Array Creation | Creating a NumPy array. | arr = np.array([1, 2, 3, 4, 5]) |
| Element-Wise Arithmetic | Element-wise addition, subtraction, and so on. | result = arr1 + arr2 |
| Scalar Arithmetic | Scalar addition, subtraction, and so on. | result = arr * 2 |
| Element-Wise Functions | Applying functions to each element. | result = np.sqrt(arr) |
| Sum and Mean | Calculating the sum and mean of an array.Calculating the sum and mean of an array. | total = np.sum(arr)<br>average = np.mean(arr) |
| Maximum and Minimum Values | Finding the maximum and minimum values. | max_val = np.max(arr)<br>min_val = np.min(arr) |
| Reshaping | Changing the shape of an array. | reshaped_arr = arr.reshape(2, 3) |
| Transposition | Transposing a multi-dimensional array. | transposed_arr = arr.T |
| Matrix Multiplication | Performing matrix multiplication. | result = np.dot(matrix1, matrix2) |
Conclusion
NumPy is a fundamental library for data science and numerical computations. This guide covers the basics of NumPy, and there's much more to explore. Visit numpy.org for more information and examples.
Author

06.API.htm
Some Context on APIs
Estimated Effort: 5 mins
What are APIs?
APIs, or Application Programming Interfaces, are a crucial part of software development. They allow developers to create new applications by leveraging existing functionality from other systems. APIs define how software components should interact and facilitate communication between various products and services without requiring direct implementation.
Importance of APIs
APIs are essential for any engineer because they provide a way to access data and functionality from other systems, which can save time and resources. For instance, APIs can be used to integrate applications into the existing architecture of a server or application, allowing developers to communicate between various products and services without requiring direct implementation.
APIs are also important because they enable developers to create new applications by leveraging existing functionality from other systems. This can help developers throughout the engineering and development process of apps.
APIs are used in a wide range of applications, from social media platforms to e-commerce websites. They are also used in mobile applications, web applications, and desktop applications.
Applications of APIs
APIs have a wide range of applications, some of which are:
Social media platforms: Social media platforms like Facebook, Twitter, and Instagram use APIs to allow developers to access their data and functionality. This allows developers to create applications that can interact with these platforms and provide additional functionality to users.
E-commerce websites: E-commerce websites like Amazon and eBay use APIs to allow developers to access their product catalogs and other data. This allows developers to create applications that can interact with these platforms and provide additional functionality to users.
Weather applications: Weather applications like AccuWeather and The Weather Channel use APIs to access weather data from various sources. This allows developers to create applications that can provide users with up-to-date weather information.
Maps and navigation applications: Maps and navigation applications like Google Maps and Waze use APIs to access location data and other information. This allows developers to create applications that can provide users with directions, traffic updates, and other location-based information.
Payment gateways: Payment gateways like PayPal and Stripe use APIs to allow developers to access their payment processing functionality. This allows developers to create applications that can process payments securely and efficiently.
Messaging applications: Messaging applications like WhatsApp and Facebook Messenger use APIs to allow developers to access their messaging functionality. This allows developers to create applications that can interact with these platforms and provide additional functionality to users.
Conclusion
In summary, APIs are an essential part of software development, and they provide a way to access data and functionality from other systems. They are used in a wide range of applications and can help developers save time and resources while creating new applications.
Author(s)
07.ChtSht.Data.htm
Working with Data in Python Cheat Sheet
Reading and writing files| Package/Method | Description | Syntax and Code Example |
|---|---|---|
| File opening modes | Different modes to open files for specific operations. | Syntax: r (reading) w (writing) a (appending) + (updating: read/write) b (binary, otherwise text)
|
| File reading methods | Different methods to read file content in various ways. | Syntax:
Example:
|
| File writing methods | Different write methods to write content to a file. | Syntax:
Example:
|
| Iterating over lines | Iterates through each line in the file using a `loop`. | Syntax:
Example:
|
| Open() and close() | Opens a file, performs operations, and explicitly closes the file using the close() method. | Syntax:
Example:
|
| with open() | Opens a file using a with block, ensuring automatic file closure after usage. | Syntax:
Example:
|
| Package/Method | Description | Syntax and Code Example |
|---|---|---|
| .read_csv() | Reads data from a `.CSV` file and creates a DataFrame. | Syntax: dataframe_name = pd.read_csv("filename.csv") Example: df = pd.read_csv("data.csv") |
| .read_excel() | Reads data from an Excel file and creates a DataFrame. | Syntax:
Example:
|
| .to_csv() | Writes DataFrame to a CSV file. | Syntax:
Example:
|
| Access Columns | Accesses a specific column using [] in the DataFrame. | Syntax:
Example:
|
| describe() | Generates statistics summary of numeric columns in the DataFrame. | Syntax:
Example:
|
| drop() | Removes specified rows or columns from the DataFrame. axis=1 indicates columns. axis=0 indicates rows. | Syntax:
Example:
|
| dropna() | Removes rows with missing NaN values from the DataFrame. axis=0 indicates rows. | Syntax:
Example:
|
| duplicated() | Duplicate or repetitive values or records within a data set. | Syntax:
Example:
|
| Filter Rows | Creates a new DataFrame with rows that meet specified conditions. | Syntax:
Example:
|
| groupby() | Splits a DataFrame into groups based on specified criteria, enabling subsequent aggregation, transformation, or analysis within each group. | Syntax:
Example:
|
| head() | Displays the first n rows of the DataFrame. | Syntax:
Example:
|
| Import pandas | Imports the Pandas library with the alias pd. | Syntax:
Example:
|
| info() | Provides information about the DataFrame, including data types and memory usage. | Syntax:
Example:
|
| merge() | Merges two DataFrames based on multiple common columns. | Syntax:
Example:
|
| print DataFrame | Displays the content of the DataFrame. | Syntax:
Example:
|
| replace() | Replaces specific values in a column with new values. | Syntax:
Example:
|
| tail() | Displays the last n rows of the DataFrame. | Syntax:
Example:
|
| Package/Method | Description | Syntax and Code Example |
|---|---|---|
| Importing NumPy | Imports the NumPy library. | Syntax:
Example:
|
| np.array() | Creates a one or multi-dimensional array, | Syntax:
Example:
|
| Numpy Array Attributes |
- Calculates the mean of array elements - Calculates the sum of array elements - Finds the minimum value in the array - Finds the maximum value in the array - Computes dot product of two arrays |
Example:
|
© IBM Corporation. All rights reserved.
08.Glossary_Data.htm
Glossary: Working with Data in Python
Welcome! This alphabetized glossary contains many of the terms you'll find within this course. This comprehensive glossary also includes additional industry-recognized terms not used in course videos. These terms are important for you to recognize when working in the industry, participating in user groups, and participating in other certificate programs.
| Term | Definition |
|---|---|
| .csv file | A .csv (Comma-Separated Values) file is a plain text file format for storing tabular data, where each line represents a row and uses commas to separate values in different columns. |
| .txt file | A .txt (Text) file is a common file format that contains plain text without specific formatting, making it suitable for storing and editing textual data. |
| Append | To "append" means to add or attach something to the end of an existing object, typically used in the context of adding data to a file or elements to a data structure like a list in Python. |
| Attribute | An "attribute" in Python refers to a property or characteristic associated with an object, which can be accessed using dot notation. |
| Broadcasting in NumPy | Broadcasting in NumPy allows arrays with different shapes to be combined in element-wise operations by automatically extending smaller arrays to match the shape of larger ones, making operations more flexible. |
| Component | In NumPy, a "component" typically refers to a specific element or value within a multi-dimensional array, which can be accessed using indexing. |
| Computation | Computation in NumPy involves performing numerical operations on arrays and matrices, making it a powerful library for mathematical and scientific computing in Python. |
| Data analysis | Data analysis is the process of inspecting, cleaning, transforming, and interpreting data to discover useful information, draw conclusions, and support decision-making. |
| DataFrames | A DataFrames in Pandas is a two-dimensional, tabular data structure for storing and analyzing data, consisting of rows and columns. |
| Dependencies | Dependencies in Pandas are external libraries or modules, such as NumPy, that Pandas rely on for fundamental data manipulation and analysis functionality. |
| File attribute | File attributes generally refer to properties or metadata associated with files, which are managed at the operating system level. |
| File object | A "file object" in Python represents an open file, allowing reading from or writing to the file. |
| Grid | In Python, a "grid" typically refers to a two-dimensional structure composed of rows and columns, often used to represent data in a tabular format or for organizing objects in a coordinate system. |
| Hadamard Product | The Hadamard product is a mathematical operation that involves element-wise multiplication of two matrices or arrays of the same shape, producing a new matrix with each element being the product of the corresponding elements in the input matrices. |
| Importing pandas | To import Pandas in Python, you use the statement: import pandas as pd, which allows you to access Pandas functions and data structures using the abbreviation "pd." |
| Index | In Python, an "index" typically refers to a position or identifier used to access elements within a sequence or data structure, such as a list or string. |
| Libraries | Libraries in Python are collections of pre-written code modules that provide reusable functions and classes to simplify and enhance software development. |
| Linespace | In Python, "linespace" refers to a NumPy function that generates an array of evenly spaced values within a specified range. |
| NumPy | NumPy in Python is a fundamental library for numerical computing that provides support for large, multi-dimensional arrays and matrices, as well as a variety of high-level mathematical functions to operate on these arrays. |
| One dimensional NumPy | A one-dimensional NumPy array is a linear data structure that stores elements in a single sequence, often used for numerical computations and data manipulation. |
| Open function | In Python, the "open" function is used to access and manipulate files, allowing you to read from or write to a specified file. |
| Pandas | Pandas is a popular Python library for data manipulation and analysis, offering data structures and tools for working with structured data like tables and time series. |
| Pandas library | Pandas library in Python refer to the various modules and functions within the Pandas library, which provides powerful data structures and data analysis tools for working with structured data. |
| Plotting Mathematical Functions | Plotting mathematical functions in Python involves using libraries like Matplotlib to create graphical representations of mathematical equations, aiding visualization, and analysis. |
| Shape | In NumPy, "shape" refers to an array's dimensions (number of rows and columns), describing its size and structure. |
| Slicing | Slicing in NumPy entails extracting specific portions of an array by specifying a range of indices, enabling you to work with subsets of the data. |
| Two dimensional NumPy | A two-dimensional NumPy array is a structured data representation with rows and columns, resembling a matrix or table, ideal for various data manipulation and analysis tasks. |
| Universal Functions | Universal functions (ufuncs) in NumPy are functions that operate element-wise on arrays, providing efficient and vectorized operations for a wide range of mathematical and logical operations. |
| Vector addition | Vector addition in Python involves adding corresponding elements of two or more vectors, producing a new vector with the sum of their components. |
| Visualizations | Visualizations in Python refer to the creation of graphical representations, such as charts, plots, and graphs, to illustrate and communicate data and trends visually. |

Module 5¶
#lnk="/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork_labs_Cheat_Sheet_Week-1.md.html"
ModuleFolder='C:/Users/Gamaliel/Documents/G/ADD/IBM_DS/Python4DS/IBMs-NBs/Mod5/'
os.chdir(ModuleFolder)
for file in os.listdir(ModuleFolder):
if file.lower().endswith('HTMLs.html'):
continue
elif file.lower().endswith('.html'):
link=file
#print(link)
display(HTML(ModuleFolder+link))
elif file.lower().endswith('.htm'):
link=file
#print(link)
display(HTML(ModuleFolder+link))
else:
continue
#for file in os.listdir(ModuleFolder):
# if not file.lower().endswith('.htm'):
# continue
# link=file
#print(link)
#display(HTML(ModuleFolder+link))

Web Scraping and HTML Basics
Estimated time: 10 mins
Objectives
After completing this reading, you will be able to:
- Explain key concepts related to HTML structure and HTML tag composition.
- Explore the concept of HTML document trees.
- Familiarize yourself with HTML tables.
- Gain insight into the basics of web scraping using Python and BeautifulSoup.
Introduction to web scraping
Web scraping, also known as web harvesting or web data extraction, is the process of extracting information from websites or web pages. It involves automated retrieval of data from web sources. People use it for various applications such as data analysis, mining, price comparison, content aggregation, and more.
How web scraping works
HTTP request
The process typically begins with an HTTP request. A web scraper sends an HTTP request to a specific URL, similar to how a web browser would when you visit a website. The request is usually an HTTP GET request, which retrieves the web page's content.
Web page retrieval
The web server hosting the website responds to the request by returning the requested web page's HTML content. This content includes the visible text and media elements and the underlying HTML structure that defines the page's layout.
HTML parsing
Once the HTML content is received, you need to parse the content. Parsing involves breaking down the HTML structure into components, such as tags, attributes, and text content. You can use BeautifulSoup in Python. It creates a structured representation of the HTML content that can be easily navigated and manipulated.
Data extraction
With the HTML content parsed, web scrapers can now identify and extract the specific data they need. This data can include text, links, images, tables, product prices, news articles, and more. Scrapers locate the data by searching for relevant HTML tags, attributes, and patterns in the HTML structure.
Data transformation
Extracted data may need further processing and transformation. For instance, you can remove HTML tags from text, convert data formats, or clean up messy data. This step ensures the data is ready for analysis or other use cases.
Storage
After extraction and transformation, you can store the scraped data in various formats, such as databases, spreadsheets, JSON, or CSV files. The choice of storage format depends on the specific project's requirements.
Automation
In many cases, scripts or programs automate web scraping. These automation tools allow recurring data extraction from multiple web pages or websites. Automated scraping is especially useful for collecting data from dynamic websites that regularly update their content.

HTML structure
Hypertext markup language (HTML) serves as the foundation of web pages. Understanding its structure is crucial for web scraping.
<html>is the root element of an HTML page.<head>contains meta-information about the HTML page.<body>displays the content on the web page, often the data of interest.<h3>tags are type 3 headings, making text larger and bold, typically used for player names.<p>tags represent paragraphs and contain player salary information.
Composition of an HTML tag
HTML tags define the structure of web content and can contain attributes.
- An HTML tag consists of an opening (start) tag and a closing (end) tag.
- Tags have names (
<a>for an anchor tag). - Tags may contain attributes with an attribute name and value, providing additional information to the tag.
HTML document tree
You can visualize HTML documents as trees with tags as nodes.
- Tags can contain strings and other tags, making them the tag's children.
- Tags within the same parent tag are considered siblings.
- For example, the
<html>tag contains both<head>and<body>tags, making them descendants of<htmlbut children of<html>.<head>and<body>are siblings.

HTML tables
HTML tables are essential for presenting structured data.
- Define an HTML table using the
<table>tag. - Each table row is defined with a
<tr>tag. - The first row often uses the table header tag, typically
<th>. - The table cell is represented by
<td>tags, defining individual cells in a row.

Web scraping
Web scraping involves extracting information from web pages using Python. It can save time and automate data collection.
Required tools
Web scraping requires Python code and two essential modules: Requests and Beautiful Soup. Ensure you have both modules installed in your Python environment.
- 1
- 2
# Import Beautiful Soup to parse the web page contentfrom bs4 import BeautifulSoup
Fetching and parsing HTML
To start web scraping, you need to fetch the HTML content of a webpage and parse it using Beautiful Soup. Here's a step-by-step example:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
import requestsfrom bs4 import BeautifulSoup# Specify the URL of the webpage you want to scrapeurl = 'https://en.wikipedia.org/wiki/IBM'# Send an HTTP GET request to the webpageresponse = requests.get(url)# Store the HTML content in a variablehtml_content = response.text# Create a BeautifulSoup object to parse the HTMLsoup = BeautifulSoup(html_content, 'html.parser')# Display a snippet of the HTML contentprint(html_content[:500])
Navigating the HTML structure
BeautifulSoup represents HTML content as a tree-like structure, allowing for easy navigation. You can use methods like find_all to filter and extract specific HTML elements. For example, to find all anchor tags () and print their text:
- 1
- 2
- 3
- 4
- 5
- 6
# Find all <a> tags (anchor tags) in the HTMLlinks = soup.find_all('a')# Iterate through the list of links and print their textfor the link in links:print(link.text)
Custom data extraction
Web scraping allows you to navigate the HTML structure and extract specific information based on your requirements. This process may involve finding specific tags, attributes, or text content within the HTML document.
Using BeautifulSoup for HTML parsing
Beautiful Soup is a powerful tool for navigating and extracting specific web page parts. It allows you to find elements based on their tags, attributes, or text, making extracting the information you're interested in easier.
Using pandas read_html for table extraction
Pandas, a Python library, provides a function called read_html, which can automatically extract data from websites' tables and present it in a format suitable for analysis. It’s similar to taking a table from a webpage and importing it into a spreadsheet for further analysis.
Conclusion
In this reading, you learned about web scraping with BeautifulSoup and Pandas with emphasis on extracting elements and tables. BeautifulSoup facilitates HTML parsing, while Pandas' read_html streamlines table extraction. The reading also highlighted responsible web scraping, ensuring adherence to website terms. Armed with this knowledge, you can confidently engage in precise data extraction.
Author
© IBM Corporation. All rights reserved.
Web Scraping: A Key Tool in Data Science
Estimated Effort: 5 mins
Introduction
Web scraping, also known as web harvesting or web data extraction, is a technique used to extract large amounts of data from websites. The data on websites is unstructured, and web scraping enables us to convert it into a structured form.
Importance of Web Scraping in Data Science
In the field of data science, web scraping plays an integral role. It is used for various purposes such as:
- Data Collection: Web scraping is a primary method of collecting data from the internet. This data can be used for analysis, research, etc.
- Real-time Application: Web scraping is used for real-time applications like weather updates, price comparison, etc.
- Machine Learning: Web scraping provides the data needed to train machine learning models.
Web Scraping with Python
Python provides several libraries for web scraping. Here are some of them:
- BeautifulSoup: BeautifulSoup is a Python library used for web scraping purposes to pull the data out of HTML and XML files. It creates a parse tree from page source code that can be used to extract data in a hierarchical and more readable manner.
- 1
- 2
- 3
- 4
- 5
from bs4 import BeautifulSoupimport requestsURL = "http://www.example.com"page = requests.get(URL)soup = BeautifulSoup(page.content, "html.parser")
- Scrapy: Scrapy is an open-source and collaborative web crawling framework for Python. It is used to extract the data from the website.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
import scrapyclass QuotesSpider(scrapy.Spider):name = "quotes"start_urls = ['http://quotes.toscrape.com/tag/humor/',]def parse(self, response):for quote in response.css('div.quote'):yield {'quote': quote.css('span.text::text').get()}
- Selenium: Selenium is a tool used for controlling web browsers through programs and automating browser tasks.
- 1
- 2
- 3
from selenium import webdriverdriver = webdriver.Firefox()driver.get("http://www.example.com")
Applications of Web Scraping
Web scraping is used in various fields and has many applications:
Price Comparison: Services such as ParseHub use web scraping to collect data from online shopping websites and use it to compare the prices of products.
Email address gathering: Many companies that use email as a medium for marketing, use web scraping to collect email ID and then send bulk emails.
Social Media Scraping: Web scraping is used to collect data from Social Media websites such as Twitter to find out what's trending.
Conclusion
Web scraping is an essential skill in the fast-growing world of data science. It provides the ability to turn the web into a source of data that can be analyzed, processed, and used for a variety of applications. However, it's important to remember that one should use web scraping responsibly and ethically, respecting the terms of use or robots.txt files of the websites being scraped.
Author(s)

Web Scraping Tables using Pandas
Estimated Effort: 5 mins
The Pandas library in Python contains a function read_html() that can be used to extract tabular information from any web page.
Consider the following example:
Let us assume we want to extract the list of the largest banks in the world by market capitalization, from the following link:
- 1
URL = 'https://en.wikipedia.org/wiki/List_of_largest_banks'
We may use pandas.read_html() function in python to extract all the tables in the web page directly.
A snapshot of the webpage is shown below.
We can see that the required table is the first one in the web page.
Note: This is a live web page and it may get updated over time. The image shown above has been captured in November 2023. The process of data extraction remains the same.
We may execute the following lines of code to extract the required table from the web page.
- 1
- 2
- 3
- 4
- 5
import pandas as pdURL = 'https://en.wikipedia.org/wiki/List_of_largest_banks'tables = pd.read_html(URL)df = tables[0]print(df)
This will extract the required table as a dataframe df. The output of the print statement would look as shown below.

Although convenient, this method comes with its own set of limitations.
Firstly, web pages may have content saved in them as tables but they may not appear as tables on the web page.
For instance, consider the following URL showing the list of countries by GDP (nominal).
- 1
URL = 'https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)'
The images on the web page are also saved in tabular format. A snapshot of the web page is shared below.
Secondly,
the contents of the tables in the web pages may contain elements such
as hyperlink text and other denoters, which are also scraped directly
using the pandas method. This may lead to a requirement of further
cleaning of data.
A closer look at table 3 in the image shown above
indicates that there are many hyperlink texts which are also going to be
treated as information by the pandas function.

We can extract the table using the code shown below.
- 1
- 2
- 3
- 4
- 5
import pandas as pdURL = 'https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)'tables = pd.read_html(URL)df = tables(2) # the required table will have index 2print(df)
The output of the print statement is shown below.

Note that the hyperlink texts have also been retained in the code output.
It is further prudent to point out, that this method exclusively operates only on tabular data extraction. BeautifulSoup library still remains the default method of extracting any kind of information from web pages.
Author(s)
Cheat Sheet: API's and Data Collection
| Package/Method | Description | Code Example |
|---|---|---|
| Accessing element attribute | Access the value of a specific attribute of an HTML element. | Syntax:
Example:
|
| BeautifulSoup() | Parse the HTML content of a web page using BeautifulSoup. The parser type can vary based on the project. | Syntax:
Example:
|
| delete() | Send a DELETE request to remove data or a resource from the server. DELETE requests delete a specified resource on the server. | Syntax:
Example:
|
| find() | Find the first HTML element that matches the specified tag and attributes. | Syntax:
Example:
|
| find_all() | Find all HTML elements that match the specified tag and attributes. | Syntax:
Example:
|
| findChildren() | Find all child elements of an HTML element. | Syntax:
Example:
|
| get() | Perform a GET request to retrieve data from a specified URL. GET requests are typically used for reading data from an API. The response variable will contain the server's response, which you can process further. | Syntax:
Example:
|
| Headers | Include custom headers in the request. Headers can provide additional information to the server, such as authentication tokens or content types. | Syntax:
Example:
|
| Import Libraries | Import the necessary Python libraries for web scraping. | Syntax:
|
| json() | Parse JSON data from the response. This extracts and works with the data returned by the API. The response.json() method converts the JSON response into a Python data structure (usually a dictionary or list). | Syntax:
Example:
|
| next_sibling() | Find the next sibling element in the DOM. | Syntax:
Example:
|
| parent | Access the parent element in the Document Object Model (DOM). | Syntax:
Example:
|
| post() | Send a POST request to a specified URL with data. Create or update POST requests using resources on the server. The data parameter contains the data to send to the server, often in JSON format. | Syntax:
Example:
|
| put() | Send a PUT request to update data on the server. PUT requests are used to update an existing resource on the server with the data provided in the data parameter, typically in JSON format. | Syntax:
Example:
|
| Query parameters | Pass query parameters in the URL to filter or customize the request. Query parameters specify conditions or limits for the requested data. | Syntax:
Example:
|
| select() | Select HTML elements from the parsed HTML using a CSS selector. | Syntax:
Example:
|
| status_code | Check the HTTP status code of the response. The HTTP status code indicates the result of the request (success, error, redirection). Use the HTTP status codeIt can be used for error handling and decision-making in your code. | Syntax:
Example:
|
| tags for find() and find_all() | Specify any valid HTML tag as the tag parameter to search for elements of that type. Here are some common HTML tags that you can use with the tag parameter. | Tag Example:
|
| text | Retrieve the text content of an HTML element. | Syntax:
Example:
|
© IBM Corporation. All rights reserved.
Glossary: APIs and Data Collection
Welcome! This alphabetized glossary contains many of the terms you'll find within this course. This comprehensive glossary also includes additional industry-recognized terms not used in course videos. These terms are important for you to recognize when working in the industry, participating in user groups, and participating in other certificate programs.
| Term | Definition |
|---|
| API Key | An API key in Python is a secure access token
or code used to authenticate and authorize access to an API or web
service, enabling the user to make authenticated requests. |
| APIs
| APIs (Application Programming Interfaces) are a set of rules and
protocols that enable different software applications to communicate and
interact, facilitating the exchange of data and functionality. |
|
Audio file |An audio file is a digital recording or representation of
sound, often stored in formats like MP3, WAV, or FLAC, allowing playback
and storage of audio content.|
|Authorize|In Python, "authorize"
often means granting permission or access to a user or system to perform
specific actions or access particular resources, often related to
authentication and authorization mechanisms.|
|Beautiful Soup
Objects|Beautiful Soup objects in Python are representations of parsed
HTML or XML documents, allowing easy navigation, searching, and
manipulation of the document’s elements and data.|
|Bitcoin
currency|Bitcoin is a decentralized digital currency that operates
without a central authority, allowing peer-to-peer transactions on a
blockchain network.|
|Browser|A browser is a software application
that enables users to access and interact with web content, displaying
websites and web applications.|
|Candlestick plot|A candlestick plot
in Python visually represents stock price movements over time, using
rectangles to illustrate the open, close, high, and low prices for a
given period.|
|Client/Wrapper|A client or wrapper in Python is a
software component that simplifies interaction with external services or
APIs, encapsulating communication and providing higher-level
functionality for developers.|
|CoinGecko API|The CoinGecko API is a
web service that provides cryptocurrency market data and information,
allowing developers to access real-time and historical data for various
cryptocurrencies.|
|DELETE Method|The DELETE method in Python is an
HTTP request method used to request the removal or deletion of a
resource on a web server.|
|Endpoint|In Python, an "endpoint" refers
to a specific URL or URI that a web service or API exposes to perform a
particular function or access a resource. |
|File extension|A file
extension is a suffix added to a filename to indicate the file's format
or type, often used by operating systems and applications to determine
how to handle the file. |
|find_all|In Python, find_all is a
Beautiful Soup method used to search and extract all occurrences of a
specified HTML or XML element, returning a list of matching elements.|
|
GET method|The GET method in Python is an HTTP request method used to
retrieve data from a web server by appending parameters to the URL.|
|
HTML|HTML (Hypertext Markup Language) is the standard language for
creating and structuring content on web pages, using tags to define the
structure and presentation of documents.|
|HTML Anchor tags|HTML
anchor tags in Python are used to create hyperlinks within web pages,
linking to other web pages or resources using the <a> element with
the href attribute.|
|HTML Tables|HTML tables in Python are used to
organize and display data in a structured grid format on a web page,
constructed with <table>, <tr>, <th>, and <td>
elements.|
|HTML Tag|An HTML tag in Python is a specific code
enclosed in angle brackets used to define elements within an HTML
document, specifying how content should be presented or structured.|
|
HTML Trees|HTML trees in Python refer to the hierarchical structure
created when parsing an HTML document, representing its elements and
their relationships, typically used for manipulation or extraction of
data.|
|HTTP|HTTP (HyperText Transfer Protocol) is the foundation of
data communication on the World Wide Web, used for transmitting and
retrieving web content between clients and servers.|
|httplib |A library that provides a set of functions and classes to send and handle HTTP and HTTPS requests.|
|
Identify|In Python, "identify" usually means determining if two
variables or objects refer to the same memory location, which can be
checked using the is operator. |
|Instance|In Python, an "instance"
typically refers to a specific occurrence of an object or class, created
from a class blueprint, with its own unique set of data and
attributes.|
|JSON file|A JSON (JavaScript Object Notation) file is a
lightweight data interchange format that stores structured data in a
human-readable text format, commonly used for configuration, data
exchange, and web APIs.|
|Mean value|The mean value in Python is the
average of a set of numerical values, calculated by adding all values
and dividing by the total number of values.|
|Navigable string|In
Python, a Navigable String is a Beautiful Soup object representing a
string within an HTML or XML document, allowing for navigation and
manipulation of the text content.|
|Plotly|Plotly is a Python library
for creating interactive and visually appealing web-based data
visualizations and dashboards.|
|PNG file|A PNG (Portable Network
Graphics) file is a lossless image format in Python that is commonly
used for high-quality graphics with support for transparency and
compression.|
|POST method|The POST method in Python is an HTTP
request method used to send data to a web server, often used for
submitting form data and creating or updating resources.|
|Post
request|A POST request in Python is an HTTP method used to send data to a
web server for the purpose of creating or updating a resource,
typically used in web applications and APIs.|
|PUT method|The PUT
method in Python is an HTTP request method used to update an existing
resource on a web server by replacing or modifying it.|
|Py-Coin-
Gecko|Py-Coin-Gecko is a Python library that provides a convenient
interface for accessing cryptocurrency data and information from the
CoinGecko API.|
|Python iterable|A Python iterable is an object that
can be looped over, typically used in for loops, and includes data
structures like lists, tuples, and dictionaries. |
|Query string|A
query string in Python is a part of a URL that contains data or
parameters to be sent to a web server, typically used in HTTP GET
requests to retrieve specific information.|
|rb mode|In Python, "rb"
mode is used when opening a file to read it in binary mode, allowing you
to read and manipulate non-text files like images or binary data.|
|
Resource|In Python, a "resource" typically refers to an external entity
such as a file, database connection, or network object that can be
managed and manipulated within a program.|
|Rest API|A REST API in
Python is a web-based interface that follows the principles of
Representational State Transfer (REST), allowing communication and data
exchange over HTTP using standard HTTP methods and data formats.|
|
Service instance|In Python, a "service instance" typically refers to an
instantiated object or entity representing a service, enabling
interaction with that service in a program or application.|
|
Timestamp|A timestamp is a representation of a specific moment in time,
often expressed as a combination of date and time, used for
record-keeping and data tracking.|
|Transcribe |"Transcribe"
typically means converting spoken language or audio into written text,
often using automatic speech recognition (ASR) technology.|
|Unix
timestamp |A UNIX timestamp is a numerical value representing the number
of seconds that have elapsed since January 1, 1970, 00:00:00 UTC, used
for time-keeping in Unix-based systems and programming.|
|url
(Uniform Resource Locator) |In Python, a URL (Uniform Resource Locator)
is a web address that specifies the location of a resource on the
internet, typically consisting of a protocol, domain, and path.|
|
urllib |The "urllib" library in Python is used for working with URLs and
making HTTP requests, including functions for fetching web content,
handling cookies, and more.|
|Web service |Web services in Python are
software components that allow applications to communicate over the
internet by sending and receiving data in a standardized format,
typically using protocols like HTTP or XML.|
|Web scraping|Web
scraping in Python is the process of extracting data from websites by
parsing and analyzing their HTML structure, often done with libraries
like BeautifulSoup or Scrapy.|
|xlsx|An XLSX file is a file format
used for storing spreadsheet data in Excel, containing worksheets,
cells, and formulas in a structured manner.|
|xml|XML (Extensible
Markup Language) is a text-based format for storing and structuring data
using tags, often used for data interchange and configuration files.|

Save document¶
#import pdfkit
# Convert an HTML file to a PDF
#!jupyter nbconvert --to pdf --no-input Notes.ipynb
ModuleFolder='C:/Users/Gamaliel/Documents/G/ADD/IBM_DS/Python4DS/IBMs-NBs/'
os.chdir(ModuleFolder)
try:
!jupyter nbconvert HTMLs.ipynb --to html --template pj
#!jupyter nbconvert --to html --template pj-pdf HTMLs.ipynb
except Exception as e:
print('HTML not stored')
import shutil
FromFld=ModuleFolder
Tofld='C:/Users/Gamaliel/Documents/MEGAsync/BC/Conducta visual/Data_Science_Notes/'
file='HTMLs.html'
file2='HTMLs.pdf'
try:
if os.path.isfile(Tofld+'/'+file):
os.remove(Tofld+'/'+file)
print(file, 'deleted in', Tofld)
shutil.move(os.path.join(FromFld,file),os.path.join(Tofld,file))
print(file, 'replaced in', Tofld)
else:
shutil.move(os.path.join(FromFld,file),os.path.join(Tofld,file))
print(file, 'written in', Tofld)
except Exception as e:
print('HTML not moved')
#try:
# if os.path.isfile(Tofld+'/'+file2):
# os.remove(Tofld+'/'+file2)
# print(file, 'deleted in', Tofld)
# shutil.move(os.path.join(FromFld,file2),os.path.join(Tofld,file2))
# print(file2, 'replaced in', Tofld)
#else:
# shutil.move(os.path.join(FromFld,file2),os.path.join(Tofld,file2))
# print(file2, 'written in', Tofld)
#except Exception as e:
# print('PDF not moved')
#shutil.copy(os.path.join(FromFld,file), Tofld)
# The line above copies all the content from A -> B
#shutil.copytree(FromFld, Tofld)
HTMLs.html deleted in C:/Users/Gamaliel/Documents/MEGAsync/BC/Conducta visual/Data_Science_Notes/ HTMLs.html replaced in C:/Users/Gamaliel/Documents/MEGAsync/BC/Conducta visual/Data_Science_Notes/
"C:/Users/Gamaliel/Documents/G/ADD/IBM_DS/Python4DS/IBMs-NBs/Mod4/04.Matrix_Math Archivos/maths_1.png"
'C:/Users/Gamaliel/Documents/G/ADD/IBM_DS/Python4DS/IBMs-NBs/Mod4/04.Matrix_Math Archivos/maths_1.png'